Search code examples
jsonkey-valuedataweavemule4array-map

mule 4 DataWeave2.0 swaping object,key and values from json payload


i'm facing an issue while trying to change data from payload and converting it to another api which requires different type of input with same values

input payload

{"metadata":
    {"attributes":
        [
        {
            "name":"FromDate",
            "value":"22-09-2021"
        },
        {
            "name":"ToDate",
            "value":"22-02-2022"
        }
        {
            "name":"Purchased",
            "value":"21-02-2021"
        }
        ]
    }
}

expected output payload

{"attributes":
    {
    "FromDate":"22-09-2021",
    "ToDate":"22-02-2022",
    "Purchased":"21-02-2021"
    }
}

i have tried the below code to transform the data but unable to acheive the result

%dw 2.0
output application/json
---
{
    "attributes": payload."metadata"."attributes" map ((item, index) -> {(item."name"):item."value"})
}

any suggestions and answers are welcome


Solution

  • You could as well do something purely using map.

    %dw 2.0
    output application/json
    ---
    attributes: {(payload.metadata.attributes map {
        ($.name): ($.value)
    })}