Search code examples
muledataweavemulesoftanypoint-studio

Dataweave transformation of converting objects with same key to array of objects


Below example contains same key "row". There may be thousands of such objects. I need an optimal solution in converting below object:

{
    "row":{
        "name": "abc"
    },
    "row":{
        "school": "pqr"
    }
}

Required output:

{
    "rows":
    [
        {
            "name": "abc"
        },
        {
            "school": "pqr"
        }
    ]
}

Solution

  • Just make use of the Multi-value Selector to get all keys with same key name:

    https://docs.mulesoft.com/dataweave/2.4/dataweave-selectors

    %dw 2.0
    output application/json
    ---
    rows: payload.*row