How do I transform the incoming JSON payload having objects to a payload having a list of the objects expected by the downstream system

48 views Asked by At

input payload:

{
  "9873452": "74298",
  "9067329": "32115"
  "1234556": "98765"
}

Expected output payload

{
    "provider": "Salesforce",
    "payload": [
        {
            "empId": 74298,
            "empName": "9873452"
        },
        {
            "empId": 32115,
            "empName": "9067329"
        },
        {
            "empId": 98765,
            "empName": "1234556"
        }
    ]
}

I tried with the below code to transform the same but not able to get the expected output payload

%dw 2.0
output application/json
---
payload mapObject ((value, key, index) -> {
provider: "Salesforce",
payload:
 [empName: key,
empId:value]
})

Thanks for any advice.

1

There are 1 answers

0
Karthik On

Modified your dataweave below. Since you expect array in output, pluck will be useful here since output of mapObject will still be an object whereas for pluck it will be an array

%dw 2.0
output application/json
---
{
"provider": "Salesforce",
"payload": payload pluck (value, key, index) -> 
 {
        "empName": key,
        "empId": value
    }
}