How to combine two arrays using flatten in DataWeave

Let us consider a scenario where two simple sub-arrays need to be combined into a single array using Mule 4 DataWeave. We can achieve this very simply using the flatten function in DataWeave 2.0.

What is the use of flatten in DataWeave?


Converts a set of subarrays (such as [ [1,2,3], [4,5], [null] ]) into a single, flattened array (such as [ 1, 2, 3, 4, 5, null ])

Note: The null arrays will be excluded in the output

Also Read: 12 Useful list of Predefined variables in Dataweave MuleSoft



Combine two arrays into a single JSON array using DataWeave flatten


Input:
[
[
{
"key":"abc"
},
{
"key":"def"
}

],
[
{
"key":"123"
},
{
"key":"456"
}

]
]

Output:
[
{
"key": "abc"
},
{
"key": "def"
},
{
"key": "123"
},
{
"key": "456"
}
]

DataWeave Code:
%dw 2.0
output application/json
---
flatten(payload)

Stay tuned to this blog for more DataWeave tutorials and MuleSoft articles. Please share with your MuleSoft friends & colleagues. To discover the free resources available on the internet visit Freeble


Post a Comment

0 Comments