---
schema_version: "1"
id: 0f5e203cdb8f8a7fba360eadceeee83c263e381797d9c4db023861ef550e0cb8
title: Define a Function that Flattens Data in a List
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dataweave-cookbook-define-function-to-flatten-list.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-define-function-to-flatten-list
source_site: docs.mulesoft.com
content_type: documentation
content_hash: a60621d0da460cda2e8b51b8ebce64e0543833a95c38112e15fe8f8fbf72be87
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
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
---

# Define a Function that Flattens Data in a List

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

When presented with nested structures of data, you might need to reduce (or "flatten") the data in
them to produce a simpler output.

This DataWeave example flattens the data in the `interests` and `contenttypes` arrays with the
function that is defined in the header, and it only outputs `contenttypes` when it is not empty.

The example uses these functions:

- `map` to go through a set elements in the input.

- `reduce` to flatten the arrays.

- `if` to conditionally display a field.

- `splitBy` to parse the input.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
fun reduceMapFor(data) = data reduce (($$ splitBy ":")[0] ++ "," ++ ($ splitBy ":")[0])
---
payload.results map() ->
{
  email: $.profile.email,
  name: $.profile.firstName,
  tags: reduceMapFor($.data.interests.tags[0]),
        (contenttypes: reduceMapFor($.data.interests.contenttypes[0])) if(sizeOf($.data.interests.contenttypes[0]) > 0)
}
```

Input JSON Payload:

```json
{
  "results": [
    {
      "profile": {
        "firstName": "john",
        "lastName": "doe",
        "email": "johndoe@demo.com"
      },
      "data": {
        "interests": [
          {
            "language": "English",
            "tags": ["digital-strategy:Digital Strategy", "innovation:Innovation"],
            "contenttypes": []
          }
        ]
      }
    },
    {
      "profile": {
        "firstName": "jane",
        "lastName": "doe",
        "email": "janedoe@demo.com"
      },
      "data": {
        "interests": [
          {
            "language": "English",
            "tags": ["tax-reform:Tax Reform", "retail-health:Retail Health"],
            "contenttypes": ["News", "Analysis", "Case studies", "Press releases"]
          }
        ]
      }
    }
  ],
  "objectsCount": 2,
  "totalCount": 2,
  "statusCode": 200,
  "errorCode": 0,
  "statusReason": "OK"
}
```

Output

```json
[
  {
    "email": "johndoe@demo.com",
    "name": "john",
    "tags": "digital-strategy,innovation"
  },
  {
    "email": "janedoe@demo.com",
    "name": "jane",
    "tags": "tax-reform,retail-health",
    "contenttypes": "News,Analysis,Case studies,Press releases"
  }
]
```

## Related Examples

- [Use Constant Directives](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-use-constant-directives)

- [Conditionally Reduce a List through a Function](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-conditional-list-reduction-via-function)

- [Define a Custom Addition Function](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-define-a-custom-addition-function)

- [Pass Functions as Arguments](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-pass-functions-as-arguments)

## See Also

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

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook)
