%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
}
}
)
Map Data with DataWeave
DataWeave 2.1 is compatible with Mule 4.1. Standard Support for Mule 4.1 ended on November 2, 2020, and this version of Mule will reach its End of Life on November 2, 2022, when Extended Support ends. Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted. MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements. |
This DataWeave 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 thebooks
array. -
as
to coerce the price data into a Number type, which ensures that the transformation generates the correct type for each element.
{
"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"
}
]
}
{
"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" and "index". Instead, it calls them through the default names: $
and $$
respectively.
%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
}
})