0
votes

Mule 3.9: How to generate the payload in JSON format from the HashMap type.

Transform the HashMap type payload to JSON array format. The sample data of HashMap

Key: Value MK1 : T1 MK2 : T2 MK3 : T3

Target format to generate in Mule from DataWeave 1.0

{
  "cars": [
    {
      "makeId": "MK1",
      "makeName": "T1"
    },
    {
      "makeId": "MK2",
      "makeName": "T2"
    },
    {
      "makeId": "MK3",
      "makeName": "T3"
    }
  ]
}

Is it possible to covert the HashMap of key and value (both string type) to be converted to the above defined JSON array format.

2

2 Answers

2
votes

This script produces the expected result:

%dw 1.0
%output application/json
---
{
    "cars": payload pluck { 
        makeId: $, 
        makeName: $$ 
    }   
}

Output:

{
  "cars": [
    {
      "makeId": "T1",
      "makeName": "MK1"
    },
    {
      "makeId": "T3",
      "makeName": "MK3"
    },
    {
      "makeId": "T2",
      "makeName": "MK2"
    }
  ]
}
0
votes

After working out a few details about map operator in Mule dataweave, i was able to resolve the issue using the below transformer.

%dw 1.0

%output application/json

"cars": payload map { makeId: $, makeName: $$ }