%dw 2.0
output application/json
---
items: payload.books map (item, index) -> {
book: item mapObject (value, key) -> {
(upper(key)): value
}
}
Map Objects
The following DataWeave examples use the mapObject
function to iterate through the keys and values of objects.
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.
First Example
This example uses both the map
and mapObject
functions to iterate through the input and set all of the keys to upper case.
The example 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.
{
"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"
}
}
]
}
Second Example
This example uses the mapObject
function to iterate through the keys and values of the object that results from using groupBy
on the payload. If some objects of the input payload have the same values in the FirstName
, LastName
and Age
keys, the DataWeave script transforms those objects into a single row in a CSV file. The remaining values in the Team Name
and Role
keys for those objects are concatenated with :
in the single CSV row.
[
{
"Sr.No.": 1,
"FirstName": "Charles",
"LastName": "Lock",
"Age": 40,
"Team Name": "Scrum team 1",
"Role": "developer"
},
{
"Sr.No.": 2,
"FirstName": "Josh",
"LastName": "Rodriguez",
"Age": 45,
"Team Name": "architecture",
"Role": "SA"
},
{
"Sr.No.": 3,
"FirstName": "Josh",
"LastName": "Rodriguez",
"Age": 45,
"Team Name": "technology",
"Role": "advisor"
},
{
"Sr.No.": 4,
"FirstName": "Josh",
"LastName": "Rodriguez",
"Age": 35,
"Team Name": "development",
"Role": "developer"
},
{
"Sr.No.": 5,
"FirstName": "Jane",
"LastName": "Rodriguez",
"Age": 30,
"Team Name": "architecture",
"Role": "SA"
},
{
"Sr.No.": 6,
"FirstName": "Jane",
"LastName": "Rodriguez",
"Age": 30,
"Team Name": "Scrum team 1",
"Role": "developer"
},
{
"Sr.No.": 7,
"FirstName": "Josh",
"LastName": "Lee",
"Age": 42,
"Team Name": "Scrum team1",
"Role": "developer"
}
]
In the previous input example, Sr.No. 2
and Sr.No. 3
, as well as Sr.No. 5
and Sr.No. 6
, refer to the same person, as they have the same first name, last name, and age. However, Sr.No. 4
refers to a different person, as it has the same first and last name but a different age.
output application/csv
---
valuesOf(
payload
groupBy ((item, index) -> (
item.FirstName ++ item.LastName ++ item.Age))
mapObject ((value, key, index) ->
(index): {
"Sr.No.": value."Sr.No." joinBy ":",
"FirstName": value.FirstName[0],
"LastName": value.LastName[0],
"Age": value.Age[0],
"Team Name": value."Team Name" joinBy ":",
"Role": value.Role joinBy ":"
})
)
The DataWeave script merges the repeated values of keys FirstName
, LastName
and Age
of the objects with key-value Sr.No. 2
and Sr.No. 3
, as well as Sr.No. 5
and Sr.No. 6
. These values are separated with ,
in the single CSV row. The values of Team Name
and Role
for those objects are concatenated with :
in the single CSV row.
Sr.No.,FirstName,LastName,Age,Team Name,Role
1,Charles,Lock,40,Scrum team 1,developer
2:3,Josh,Rodriguez,45,architecture:technology,SA:advisor
4,Josh,Rodriguez,35,development,developer
5:6,Jane,Rodriguez,30,architecture:Scrum team 1,SA:developer
7,Josh,Lee,42,Scrum team1,developer