---
schema_version: "1"
id: 05cc6221773a232c64adb85bba60c823ba40f5c2252a9302376a965d3ebd2597
title: Map Objects
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.11/dataweave-cookbook-map-an-object.md
canonical_url: https://docs.mulesoft.com/dataweave/2.11/dataweave-cookbook-map-an-object
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 42cd6ba6400a305a696569eb86475e671f9c9bcc1d604e74cbea77189e5a8543
description: MuleSoft Documentation Site
release: "2.11"
latest: false
---
---
product: DataWeave
version: 2.11
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

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

The following DataWeave examples use the `mapObject` function to iterate through the keys and values
of objects.

## First Example

This example uses both the `map` and `mapObject` functions to iterate through the input and set all
of the keys to upper case.

The example uses these DataWeave functions:

- `map` to go through the elements in the "books" array.

- `mapObject` to go through the keys and values in each of the objects of the array.

- `upper` to set each key to upper case.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
---
items: payload.books map (item, index) -> {
      book: item mapObject (value, key) -> {
      (upper(key)): value
      }
}
```

Input JSON Payload:

```json
{
  "books": [
    {
      "-category": "cooking",
      "title": "Everyday Italian",
      "author": "Giada De Laurentiis",
      "year": "2005",
      "price": "30.00"
    },
    {
      "-category": "children",
      "title": "Harry Potter",
      "author": "J K. Rowling",
      "year": "2005",
      "price": "29.99"
    },
    {
      "-category": "web",
      "title": "XQuery Kick Start",
      "author": [
        "James McGovern",
        "Per Bothner",
        "Kurt Cagle",
        "James Linn",
        "Vaidyanathan Nagarajan"
      ],
      "year": "2003",
      "price": "49.99"
    },
    {
      "-category": "web",
      "-cover": "paperback",
      "title": "Learning XML",
      "author": "Erik T. Ray",
      "year": "2003",
      "price": "39.95"
    }
  ]
}
```

Output JSON:

```json
{
  "items": [
    {
      "book": {
        "-CATEGORY": "cooking",
        "TITLE": "Everyday Italian",
        "AUTHOR": "Giada De Laurentiis",
        "YEAR": "2005",
        "PRICE": "30.00"
      }
    },
    {
      "book": {
        "-CATEGORY": "children",
        "TITLE": "Harry Potter",
        "AUTHOR": "J K. Rowling",
        "YEAR": "2005",
        "PRICE": "29.99"
      }
    },
    {
      "book": {
        "-CATEGORY": "web",
        "TITLE": "XQuery Kick Start",
        "AUTHOR": [
          "James McGovern",
          "Per Bothner",
          "Kurt Cagle",
          "James Linn",
          "Vaidyanathan Nagarajan"
        ],
        "YEAR": "2003",
        "PRICE": "49.99"
      }
    },
    {
      "book": {
        "-CATEGORY": "web",
        "-COVER": "paperback",
        "TITLE": "Learning XML",
        "AUTHOR": "Erik T. Ray",
        "YEAR": "2003",
        "PRICE": "39.95"
      }
    }
  ]
}
```

## Second Example

This example uses the `mapObject` function to iterate through the keys and values of the object that
results from using `groupBy` on the payload. If some objects of the input payload have the same
values in the `FirstName`, `LastName` and `Age` keys, the DataWeave script transforms those objects
into a single row in a CSV file. The remaining values in the `Team Name` and `Role` keys for those
objects are concatenated with `:` in the single CSV row.

Input JSON Payload:

```json
[
  {
    "Sr.No.": 1,
    "FirstName": "Charles",
    "LastName": "Lock",
    "Age": 40,
    "Team Name": "Scrum team 1",
    "Role": "developer"
  },
  {
    "Sr.No.": 2,
    "FirstName": "Josh",
    "LastName": "Rodriguez",
    "Age": 45,
    "Team Name": "architecture",
    "Role": "SA"
  },
  {
    "Sr.No.": 3,
    "FirstName": "Josh",
    "LastName": "Rodriguez",
    "Age": 45,
    "Team Name": "technology",
    "Role": "advisor"
  },
  {
    "Sr.No.": 4,
    "FirstName": "Josh",
    "LastName": "Rodriguez",
    "Age": 35,
    "Team Name": "development",
    "Role": "developer"
  },
  {
    "Sr.No.": 5,
    "FirstName": "Jane",
    "LastName": "Rodriguez",
    "Age": 30,
    "Team Name": "architecture",
    "Role": "SA"
  },
  {
    "Sr.No.": 6,
    "FirstName": "Jane",
    "LastName": "Rodriguez",
    "Age": 30,
    "Team Name": "Scrum team 1",
    "Role": "developer"
  },
  {
    "Sr.No.": 7,
    "FirstName": "Josh",
    "LastName": "Lee",
    "Age": 42,
    "Team Name": "Scrum team1",
    "Role": "developer"
  }
]
```

In the previous input example, `Sr.No. 2` and `Sr.No. 3`, as well as `Sr.No. 5` and `Sr.No. 6`,
refer to the same person, as they have the same first name, last name, and age. However, `Sr.No. 4`
refers to a different person, as it has the same first and last name but a different age.

DataWeave Script:

```dataweave
output application/csv
---
valuesOf(
    payload
        groupBy ((item, index) -> (
            item.FirstName ++ item.LastName ++ item.Age))
        mapObject ((value, key, index) ->
            (index): {
                "Sr.No.": value."Sr.No." joinBy ":",
                "FirstName": value.FirstName[0],
                "LastName": value.LastName[0],
                "Age": value.Age[0],
                "Team Name": value."Team Name" joinBy ":",
                "Role": value.Role joinBy ":"
            })
)
```

The DataWeave script merges the repeated values of keys `FirstName`, `LastName` and `Age` of the
objects with key-value `Sr.No. 2` and `Sr.No. 3`, as well as `Sr.No. 5` and `Sr.No. 6`. These values
are separated with `,` in the single CSV row. The values of `Team Name` and `Role` for those objects
are concatenated with `:` in the single CSV row.

Output CSV:

```CSV
Sr.No.,FirstName,LastName,Age,Team Name,Role
1,Charles,Lock,40,Scrum team 1,developer
2:3,Josh,Rodriguez,45,architecture:technology,SA:advisor
4,Josh,Rodriguez,35,development,developer
5:6,Jane,Rodriguez,30,architecture:Scrum team 1,SA:developer
7,Josh,Lee,42,Scrum team1,developer
```

## Related Examples

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

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

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

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

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

## See Also

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

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

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