1
votes

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?

3

3 Answers

0
votes

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: enter image description here

0
votes

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...

0
votes

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)
}