---
schema_version: "1"
id: 2d49a4021cf0e32d7b86d445950f6a5d8a6b6be87be96c9724a4c4cbc3a453c6
title: Map Data with DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.12/dataweave-cookbook-map.md
canonical_url: https://docs.mulesoft.com/dataweave/2.12/dataweave-cookbook-map
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d465d34e35fcf365f35e02ffffda5a485291cd958eeda3de931930f9a26db1b4
description: MuleSoft Documentation Site
release: "2.12"
latest: false
---
---
product: DataWeave
version: 2.12
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 Data with DataWeave

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

This DataWeave example uses the DataWeave `map` function to iterate through an array of books and
perform a series of tasks on each.

The example uses these DataWeave functions:

- `map` to go through each object in the `books` array.

- `as` to coerce the price data into a Number type, which ensures that the transformation generates
  the correct type for each element.

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"
      }
    }
  ]
}
```

> [!TIP] Note that when a book has multiple authors, `item.author` evaluates to the entire array of
> authors instead of a single name.

## Using Default Values

The following example performs the same transformation as above, but it doesn’t explicitly define
the properties "item" and "index". Instead, it calls them through the default names: `$` and `$$`
respectively.

DataWeave Script:

```dataweave
%dw 2.0
output application/json
---
items: (payload.books map {
      category: "book",
      price: $.price as Number,
      id: $$,
      properties: {
        title: $.title,
        author: $.author,
        year: $.year as Number
      }
})
```

Input JSON Payload:

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

Output JSON:

```json
{
  "items": [
    {
      "category": "book",
      "price": 30.0,
      "id": 0,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "Everyday Italian"
        },
        "author": "Giada De Laurentiis",
        "year": 2005
      }
    },
    {
      "category": "book",
      "price": 29.99,
      "id": 1,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "Harry Potter"
        },
        "author": "J K. Rowling",
        "year": 2005
      }
    },
    {
      "category": "book",
      "price": 49.99,
      "id": 2,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "XQuery Kick Start"
        },
        "author": [
          "James McGovern",
          "Per Bothner",
          "Kurt Cagle",
          "James Linn",
          "Vaidyanathan Nagarajan"
        ],
        "year": 2003
      }
    },
    {
      "category": "book",
      "price": 39.95,
      "id": 3,
      "properties": {
        "title": {
          "-lang": "en",
          "#text": "Learning XML"
        },
        "author": "Erik T. Ray",
        "year": 2003
      }
    }
  ]
}
```

## Related Examples

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

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

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

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

- [Map an Object](https://docs.mulesoft.com/dataweave/2.12/dataweave-cookbook-map-an-object)

## See Also

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

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

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