%dw 2.0
output application/json
---
items: payload.books map (item, index) -> {
book: item mapObject (value, key) -> {
(upper(key)): value
}
}
Map Data with DataWeave
This DataWeave example uses the DataWeave map
function to iterate through an array of books and perform a series of tasks on each.
Before you begin, note that 2.x versions of DataWeave are used by Mule 4 apps. For
DataWeave in Mule 3 apps, refer to
DataWeave version 1.2 examples.
For other DataWeave versions, you can use the version selector in the DataWeave table of contents.
The example 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":"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"
}
]
}
{
"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"
}
}
]
}
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
}
})
{
"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
}
}
]
}