Nested Json to Flat Json

This example show usage of DataWeave to transform nested json structure to flat json structure. i break them into step 1 and 2 to better illustrate the

Input Json:

After Step 1:

After Step 2:

Step 1:
Using nested map to 1st outer loop at Orders, then 2nd inner loop at Items, finally 3rd inner loop at Variants.
At Orders and Items map, note that after the “->”, is using parentheses (), this is to output array of array only, without array’s elements.
At Variants map, after the “->” is using curly bracket {} to output array elements.

Step 2:
Adding flatten to convert nested array of arrays to flat. Here have to use flatten 2 times, because there are 2 layers of conversion:
layer 1: flatten Order’s Items
layer 2: flatten Item’s Variants

Nested Xml to Flat Xml

This is another example doing similar thing, but for Xml.

Input XML:

Output XML:

DataWeave code:
For XML, loop through each layer (Order, Item and Variant) using multi-value selector (.*<key-name>). Note that there is no need to use flatten operator that expect an array.

DataWeave – Transform Nested Structure to Flat using map and flatten
Tagged on:             

One thought on “DataWeave – Transform Nested Structure to Flat using map and flatten

Leave a Reply

Your email address will not be published. Required fields are marked *