Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Example: Mapping Objects

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

It 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

         
      
1
2
3
4
5
6
7
8
%dw 2.0
output application/json
---
items: payload.books map (item, index) -> {
      book: item mapObject (value, key) -> {
      (upper(key)): value
      }
}
Input

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
    "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

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
  "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"
      }
    }
  ]
}

In this topic: