---
schema_version: "1"
id: c277015eaf796245a2a68f8060ff6b9b6f234a16170a9580e96d4f6d34990dae
title: Flatten Elements of Arrays
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dataweave-cookbook-flatten-arrays.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-flatten-arrays
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 54df795c94509e7c8968540c2f945c86862deb76f76d08e5230a8aaad68a3d9c
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, xml, json, csv, pojo, java object, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Flatten Elements of Arrays

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

DataWeave can flatten subarrays of an array and collections of key-value pairs within DataWeave
objects, arrays, and subarrays.

## 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 Script:

```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:

```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 Script:

```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:

```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](#ex02) 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 Script:

```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:

```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 Script:

```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:

```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 Script:

```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:

```json
[
  1,
  2,
  3,
  {
    "a": "b"
  },
  "my string",
  [4, 5],
  {
    "c": "d"
  },
  6,
  null,
  {
    "e": "f"
  }
]
```

## See Also

- [flatten](https://docs.mulesoft.com/dataweave/2.6/dw-core-functions-flatten)

- [Define a Function That Flattens Data in a List](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-define-function-to-flatten-list)

- [Selectors](https://docs.mulesoft.com/dataweave/2.6/dataweave-selectors)
