---
schema_version: "1"
id: c2304c30e77e48eac1b49d926ff6c6412941ffac96fdb6f8dabd6cf464dd8ec8
title: Map Objects Key
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dataweave-cookbook-map-an-object-key.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-map-an-object-key
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 9e80454e51061a83c5ee4d760d773004b6c54b15e7b701f932a63e7fd82588c6
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
keywords: studio, anypoint, transform, transformer, format, aggregate, rename, split, filter convert, json, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping, as
---

# Map Objects Key

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

This DataWeave example uses the `mapObject` function to iterate through an array of objects and
appends a new object that matches the value of the specified criteria.

The input consists of a JSON object with sheets. The value of every sheet (for example, `(sheet)1`,
`(sheet)2`) is an array of objects. The DataWeave script adds another object `append` as the last
item to the matched sheet array if the sheet number equals to the value of the `Id` key of the
`append` object. The example uses the function `fun extractNumber` to extract the sheet number from
the sheet page name `pageName`. Then the DataWeave function `mapObject` iterates over every sheet in
the root, and if the `extractNumber` equals to the `append.Id` value, the function adds the `append`
object to the matched sheet object.

DataWeave Script:

```dataweave
%dw 2.0
output application/json

var append =
    {
    "Id": "2",
    "Access": "4444",
    "Subteam": "1",
    }

fun extractNumber(pageName: Key) =
     (pageName as String match  /\(sheet\)([0-9]+)/)[1]
---
payload mapObject ((value, key, index) -> do {
        if(extractNumber(key) == append.Id)
            {(key): value << append}
         else
            {(key): value}
})
```

Input JSON Payload:

```json
{
  "(sheet)1": [
    {
      "Id": 1.0,
      "Team": "Mule",
      "timestamp": "2019-09-26T16:37:54",
      "Access": 12431
    }
  ],
  "(sheet)2": [
    {
      "Id": 2.0,
      "Team": "Max",
      "timestamp": "2019-09-26T16:37:54",
      "Access": 81243
    },
    {
      "Id": 2.0,
      "Team": "Max Mule",
      "timestamp": "2019-09-26T18:00:54",
      "Access": 67676
    }
  ]
}
```

Output JSON:

```json
{
  "(sheet)1": [
    {
      "Id": 1.0,
      "Team": "Mule",
      "timestamp": "2019-09-26T16:37:54",
      "Access": 12431
    }
  ],
  "(sheet)2": [
    {
      "Id": 2.0,
      "Team": "Max",
      "timestamp": "2019-09-26T16:37:54",
      "Access": 81243
    },
    {
      "Id": 2.0,
      "Team": "Max Mule",
      "timestamp": "2019-09-26T18:00:54",
      "Access": 67676
    },
    {
      "Id": "2",
      "Access": "4444",
      "Subteam": "1"
    }
  ]
}
```

## Related Examples

- [Map Data with DataWeave](https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-map)

- [Map Object Elements as an Array](https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-map-object-elements-as-an-array)

- [Extract Data](https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-extract-data)

- [Transform XML to JSON](https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-perform-basic-transformation)

- [Rename Keys](https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-rename-keys)

## See Also

- [DataWeave Types](https://docs.mulesoft.com/dataweave/latest/dataweave-types)

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

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