---
schema_version: "1"
id: 9bf065d7025976cb1c0ffa948c9b87cd2086ccc07591666878a31bec08ca1084
title: flatten
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.12/dw-core-functions-flatten.md
canonical_url: https://docs.mulesoft.com/dataweave/2.12/dw-core-functions-flatten
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0de22639c8a148446bace6094daae7a883118c755b214db9734d09d397d1c33d
description: MuleSoft Documentation Site
release: "2.12"
latest: false
---
---
product: DataWeave
version: 2.12
---

# flatten

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

## flatten<T, Q>(@StreamCapable items: Array<Array<T> | Q>): Array<T | Q>

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

Note that it flattens only the first level of subarrays and omits empty subarrays.

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>items</code></p></td><td><p>The input array of arrays made up of any supported types.</p></td></tr></tbody></table>

### Example

This example defines three arrays of numbers, creates another array containing those three arrays,
and then uses the flatten function to convert the array of arrays into a single array with all
values.

#### Source

```DataWeave
%dw 2.0
output application/json
var array1 = [1,2,3]
var array2 = [4,5,6]
var array3 = [7,8,9]
var arrayOfArrays = [array1, array2, array3]
---
flatten(arrayOfArrays)
```

#### Output

```JSON
[ 1,2,3,4,5,6,7,8,9 ]
```

### Example

This example returns a single array from nested arrays of objects.

#### Source

```DataWeave
%dw 2.0
var myData =
{ user : [
   {
     group : "dev",
     myarray : [
       { name : "Shoki", id : 5678 },
       { name : "Mariano", id : 9123 }
     ]
   },
   {
     group : "test",
     myarray : [
       { name : "Sai", id : 2001 },
       { name : "Peter", id : 2002 }
     ]
   }
 ]
}
output application/json
---
flatten(myData.user.myarray)
```

#### Output

```JSON
[
  {
    "name": "Shoki",
    "id": 5678
  },
  {
    "name": "Mariano",
    "id": 9123
  },
  {
    "name": "Sai",
    "id": 2001
  },
  {
    "name": "Peter",
    "id": 2002
  }
]
```

Note that if you use `myData.user.myarray` to select the array of objects in `myarray`, instead of
using `flatten(myData.user.myarray)`, the output is a nested array of objects:

```JSON
[
  [
    {
      "name": "Shoki",
      "id": 5678
    },
    {
      "name": "Mariano",
      "id": 9123
    }
  ]
]
```

## flatten(@StreamCapable value: Null): Null

Helper function that enables `flatten` to work with a `null` value.
