Free MuleSoft CONNECT Keynote & Expo Pass Available!

Register now+
Nav

Example: Mapping Data with DataWeave

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

It 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

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
output application/json
---
items: payload.books map (item, index) -> {
      category: "book",
      price: item.price as Number,
      id: index,
      properties: {
        title: item.title,
        author: item.author,
        year: item.year as Number
      }
}
Input JSON

         
      
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
48
49
50
51
{
    "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

         
      
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{
  "items": [
    {
      "category": "book",
      "price": 30.00,
      "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
      }
    }
  ]
}
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" ad "index". Instead, it calls them through the default names: $ and $$ respectively.

DataWeave

         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
%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
      }
})