简体   繁体   中英

mule : how to convert list of maps to list of JSon in dataweave?

I have input data as list of maps like [{id="200" name="aaa"},{id="100",name="shbd"}] . I want to transform it to JSON like below

{
[
  {
    id="200",
    name="aaa"
  },
 {
    id="100",
    name="shbd"
  }
]
}

If the fields(keys in map) do no change, then it is simple and straightforward. Now how to transform if I dont know the key values. For eg, what if after sometime the input of map is [{"age":90},{"age","45"}]

It's always better to do specific mapping but you can go with the following , it will transform it into JSON

%dw 1.0
%output application/json
---
payload

As Anirban said, please validate the json you want to transform. You can use below transformation for o/p specified below:

Transformation
---------------
%dw 1.0
%output application/json
---
payload map {
    "id" : $.id,
    "name" : $.name
}


---------------
expected output
--------------
[
  {
    id="200",
    name="aaa"
  },
 {
    id="100",
    name="shbd"
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM