简体   繁体   中英

Converting multiple JSON object to one list Mule dataweave

I am having input as below

 { "ResidentialAddress": { "type": "RES", "Building": "String" }, "Name": "Satheesh", "Status": "Active", "OfficeAddress": { "type": "OFC", "Building": "String" }, "TempAddress": { "type": "TEMP", "Building": "String" } } 

I am looking to convert it as below

 {Address:[ { "type": "RES", "Building": "String" }, { "type": "OFC", "Building": "String" },{ "type": "TEMP", "Building": "String" } ]} 

When i tried with address:payload.ResidentialAddress ++ payload.TempAddress it give me combined fields not a list can anyone help?

In dataweave use the flatten operation. after dataweave set payload .

     %dw 1.0
 %output application/json
 ---
(flatten payload) filter ($.type != null)

final xml file would be

<dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[ %dw 1.0
 %output application/json
 ---
 (flatten payload) filter ($.type != null)]]></dw:set-payload>
        </dw:transform-message>
        <set-payload value="{&quot;Address&quot;: #[payload]}" mimeType="application/json" doc:name="Set Payload"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>

Output: 在此处输入图片说明

This should work for filtering any fields that have string/integer/boolean values:

%dw 1.0
%output application/json
---

{
 Address: (flatten payload) filter ($ is :object)
}

Against your test data:

%dw 1.0
%output application/json
---
{ Address:payload filter $ is :object map $}

gives:

 {
  "Address": [
    {
      "type": "RES",
      "Building": "String"
    },
    {
      "type": "OFC",
      "Building": "String"
    },
    {
      "type": "TEMP",
      "Building": "String"
    }
  ]
}

but you may need to tweak the filter to work with your real data...

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