Contact Free trial Login

Flatten Elements of Arrays

DataWeave can flatten subarrays of an array and collections of key-value pairs within DataWeave objects, arrays, and subarrays. Before you begin, note that DataWeave 2.0 (%dw 2.0) is for Mule 4 apps. For a Mule 3 app, refer to DataWeave 1.0 (%dw 1.0) examples, within the Mule 3.9 documentation set. For other Mule versions, you can use the Mule Runtime version selector in the table of contents.

Flatten Subarrays with Key-Value Pairs into an Array of Objects

This example shows how flatten acts on key-value pairs of the input array defined by the variable arrayOne. Notice that every key-value pair in the array becomes a separate DataWeave object.

The example uses this function:

  • flatten to move the elements from the subarrays to the parent array, eliminate the subarrays, and covert all key-value pairs into a list of objects within the parent array.

DataWeave:
%dw 2.0
var arrayOne = [
                 [
                    "keyOne" : 1,
                    "keyTwo" : 2
                 ],
                 [
                    "keyThree" : 3,
                    "keyFour" : 4,
                    "keyFive" : 5
                 ],
                 "keySix" : 6
               ]
output application/json
---
flatten(arrayOne)
Output JSON:
[
  {
    "keyOne": 1
  },
  {
    "keyTwo": 2
  },
  {
    "keyThree": 3
  },
  {
    "keyFour": 4
  },
  {
    "keyFive": 5
  },
  {
    "keySix": 6
  }
]

Flatten Combined Arrays

Like the previous DataWeave example, the following returns an array that combines and flattens the elements from two arrays. This second example applies the selector fruit to select only the values of the key fruit and to exclude other values in the array.

This example uses:

  • ++ to combine the objects of two arrays (arrayOne and arrayTwo) into a single array.

  • flatten to flatten the combined array into a list of key-value pairs.

DataWeave:
%dw 2.0
var arrayOne = [
                  [
                     "fruit" : "orange",
                     "fruit" : "apple"
                  ],
                  [
                     "fruit" : "grape",
                     "notfruit" : "something else"
                  ]
               ]
var arrayTwo = [
                  [
                  	{  "fruit" : "kiwi" }
                  ],
                  "fruit" : "strawberry",
                  "fruit" : "plum",
                  { "fruit" : "banana" },
                  "notfruit" : "something else"
               ]
output application/json
---
flatten(arrayOne ++ arrayTwo)
Output JSON:
[
  { "fruit": "orange" },
  { "fruit": "apple" },
  { "fruit": "grape" },
  { "notfruit": "something else" },
  { "fruit": "kiwi" },
  { "fruit": "strawberry" },
  { "fruit": "plum" },
  { "fruit": "banana" },
  { "notfruit": "something else" }
]

The only difference between the previous example and the following example is the addition of the .fruit selector to the body expression to select all the fruit values and to exclude values of the notfruit keys from the output array.

DataWeave:
%dw 2.0
var arrayOne = [
                  [
                     "fruit" : "orange",
                     "fruit" : "apple"
                  ],
                  [
                     "fruit" : "grape",
                     "notfruit" : "something else"
                  ]
               ]
var arrayTwo = [
                  [
                  	{  "fruit" : "kiwi" }
                  ],
                  "fruit" : "strawberry",
                  "fruit" : "plum",
                  { "fruit" : "banana" },
                  "notfruit" : "something else"
               ]
output application/json
---
flatten(arrayOne ++ arrayTwo).fruit
Output JSON:
[
  "orange",
  "apple",
  "grape",
  "kiwi",
  "strawberry",
  "plum",
  "banana"
]

The next example uses ..*fruit as a selector in the body expression instead of .fruit to return all nested fruit values in the flattened array. It also flattens three combined arrays instead of two.

DataWeave:
%dw 2.0
var arrayOne = [
                  [
                     "fruit" : "orange",
                     "fruit" : "apple"
                  ],
                  [
                     "fruit" : "grape",
                     "notfruit" : "something else"
                  ]
               ]
var arrayTwo = [
                  [
                  	{  "fruit" : "kiwi" }
                  ],
                  "fruit" : "strawberry",
                  "fruit" : "plum",
                  { "fruit" : "banana" },
                  "notfruit" : "something else"
               ]
var arrayThree = [
                    { parentOne :
                        [
                     	     { child :
                     	       [
                     	         { grandchild :
                                   {
                                      "fruit" : "watermelon",
                                      "notfruit" : "something else"
                                   }
                                },
                                {
                                   fruit : "cantaloupe",
                                   "notfruit" : "something else"
                                }
                             ]
                           },
                           {
                              fruit : "honeydew",
                              "notfruit" : "something else"
                           }
                        ]
                    },
                    { parentTwo:
                     	  [
                     	  	 fruit : "cherry",
                     		   "notfruit" : "something else"
                     	  ]
                    }
                 ]
output application/json
---
flatten(arrayOne ++ arrayTwo ++ arrayThree)..*fruit
Output JSON:
[
  "orange",
  "apple",
  "grape",
  "kiwi",
  "strawberry",
  "plum",
  "banana",
  "watermelon",
  "cantaloupe",
  "honeydew",
  "cherry"
]

Flatten Subarrays into the Parent Array

This example shows how the flatten function acts on a variety of data types, including numbers, DataWeave objects, subarrays, a string, a null value, and and a key-value pair. Notice that elements of the subarrays become elements of the parent array, and the subarrays are no longer present.

The example uses this function:

  • flatten to flatten the subarrays into their parent arrays.

DataWeave:
%dw 2.0
var myArray = [
                 1,
                 [2,3],
                 { a : "b"},
                 "my string",
                 [ [4,5], { c : "d"}, 6 ],
                 null,
                 "e" : "f"
              ]
output application/json
---
flatten(myArray)
Output JSON:
[
  1,
  2,
  3,
  {
    "a": "b"
  },
  "my string",
  [
    4,
    5
  ],
  {
    "c": "d"
  },
  6,
  null,
  {
    "e": "f"
  }
]

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub