Aseem
Aseem

Reputation: 1

Generating all possible combination from two json array list using dataweave

I am working on mule 3.9. While working i came across a scenario, where i want to generate all possible combination of vales from incoming JSON array list. The values under json array list are dynamic.

I want to generate the output in the xml format. I want to achieve this only using dataweave. Please help me how to achieve this in mule 3.9 using datawevae.

Below is my JSON input and required xml output.

Note: Json arrray list are dynamic

JSON Input

 "building":[  
      {  
         "code":"BuildingExcess",
         "value":"600"
      },
      {  
         "code":"BuildingExcess",
         "value":"700"
      }
   ],
"content":[  
      {  
         "code":"ContentExcess",
         "value":"600"
      },
      {  
         "code":"ContentExcess",
         "value":"400"
      }
]

Output

  <resultset>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>600</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>600</value>
        </Content>
    </list>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>700</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>600</value>
        </Content>
    </list>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>600</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>400</value>
        </Content>
    </list>
    <list>
        <Building>
            <code>BuildingExcess</code>
            <value>700</value>
        </Building>
        <Content>
            <code>ContentExcess</code>
            <value>400</value>
        </Content>
    </list> 
</resultset>

Upvotes: 0

Views: 474

Answers (1)

Brad Cooper
Brad Cooper

Reputation: 379

Assuming your two lists are always building and content, and they're elements of your payload (or some variable), you can use the following dataweave code to achieve your desired output:

%dw 1.0
%output application/xml
%var input = {
    "building":[  
          {  
             "code":"BuildingExcess",
             "value":"600"
          },
          {  
             "code":"BuildingExcess",
             "value":"700"
          }
       ],
    "content":[  
          {  
             "code":"ContentExcess",
             "value":"600"
          },
          {  
             "code":"ContentExcess",
             "value":"400"
          }
    ]   
}
---
resultset: {( 
    flatten (input.content map ((cont) -> input.building map 
        list: {
            Building: $,
            Content: cont
        })
    )
)}

Output:

<?xml version='1.0' encoding='windows-1252'?>
<resultset>
  <list>
    <Building>
      <code>BuildingExcess</code>
      <value>600</value>
    </Building>
    <Content>
      <code>ContentExcess</code>
      <value>600</value>
    </Content>
  </list>
  <list>
    <Building>
      <code>BuildingExcess</code>
      <value>700</value>
    </Building>
    <Content>
      <code>ContentExcess</code>
      <value>600</value>
    </Content>
  </list>
  <list>
    <Building>
      <code>BuildingExcess</code>
      <value>600</value>
    </Building>
    <Content>
      <code>ContentExcess</code>
      <value>400</value>
    </Content>
  </list>
  <list>
    <Building>
      <code>BuildingExcess</code>
      <value>700</value>
    </Building>
    <Content>
      <code>ContentExcess</code>
      <value>400</value>
    </Content>
  </list>
</resultset>

Upvotes: 2

Related Questions