2

I have a requirement wherein I have to convert JSON data from one format to other. I have to fetch corresponding values of JSON array and make them a key value pair.

Below are the required details:

Input:

"Headers": {
    "Header": [
      {
        "Key": "SellerOrganization",
        "Value": "XYZ"
      },
      {
        "Key": "SellerType",
        "Value": "B2C"
      },
      {
        "Key": "Region",
        "Value": "SOUTH"
      },
      {
        "Key": "OrderType",
        "Value": "RETURN"
      },
      {
        "Key": "InvoiceType",
        "Value": ""
      },
      {
        "Key": "EventType",
        "Value": "Created"
      },
      {
        "Key": "EntryType",
        "Value": "Call Center"
      }
    ]
  }

Expected Output:

{
    SellerOrganization:XYZ,
    SellerType: B2C,
    Region:SOUTH,
    OrderType:RETURN,
    InvoiceType:"",
    EventType:Created,
    EntryType:Call Center
}
Manvitha
  • 21
  • 3

2 Answers2

4

You can use the dynamic object that it will basically do what you want.

%dw 2.0
output application/json
---
{
    (payload.Headers.Header map ((item, index) -> {
        (item.Key): item.Value
    })
    )
}
machaval
  • 4,969
  • 14
  • 20
3

You can take advantage of reduce function here which will let you convert your array to an key, value pair object

%dw 2.0
output application/json
---
payload.Header reduce ((item, acc = {}) -> acc ++ {
    (item.Key): item.Value
})
Imtiyaz Qureshi
  • 281
  • 1
  • 3