---
schema_version: "1"
id: 4e92280a5a387410d0db9a0b1d1f416aa8d71947cc81adb525e2ea025b6a136a
title: Map Data with DataWeave
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dataweave-cookbook-map.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dataweave-cookbook-map
source_site: docs.mulesoft.com
content_type: documentation
content_hash: bdf2b747b3429b9d93db9537cbc5236a61c37fe22b7d0e3e26010b8b8179b13d
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 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/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)

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

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

## 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)
