%dw 2.0
var firstInput = [
{ "bookId":"101",
"title":"world history",
"price":"19.99"
},
{
"bookId":"202",
"title":"the great outdoors",
"price":"15.99"
}
]
var secondInput = [
{
"bookId":"101",
"author":"john doe"
},
{
"bookId":"202",
"author":"jane doe"
}
]
output application/json
---
firstInput map (firstInputValue) ->
{
theId : firstInputValue.bookId as Number,
theTitle: firstInputValue.title,
thePrice: firstInputValue.price as Number,
(secondInput filter ($.*bookId contains firstInputValue.bookId) map (secondInputValue) -> {
theAuthor : secondInputValue.author
})
}
Merge Fields from Separate Objects
The DataWeave examples merge fields from separate input arrays. The first (firstInput
) is a DataWeave variable that lists price by book ID, and the second (secondInput
) lists authors by book ID. Each book has a unique bookId
key.
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 DataWeave scripts produce the same output. Both scripts use two map
functions with a filter, and one of them also creates an alias for the bookId
:
-
The first
map
function iterates over the elements of thefirstInput
array. As the function evaluates each element, a secondmap
function uses a filter to identify any elements in the second array (secondInput
) that match thefilter
criteria (secondInput filter ($.*bookId contains firstInputValue.bookId)
). The filter returns an element fromsecondInput
that contains abookId
value that matches thebookId
value in thefirstInput
element. -
The second
map
function evaluates that filtered element and then usessecondInputValue.author
to select and populate the value of its"author"
field in the object{author : secondInputValue.author}
. -
filter
limits the scope of the secondmap
function to objects in thesecondInput
that share the samebookId
.
[
{
"theId": 101,
"theTitle": "world history",
"thePrice": 19.99,
"theAuthor": "john doe"
},
{
"theId": 202,
"theTitle": "the great outdoors",
"thePrice": 15.99,
"theAuthor": "jane doe"
}
]
As the next script shows, you can also write the same script using an id
alias (created with using (id = firstInputValue.bookId)
). The alias replaces the selector expression, firstInputValue.bookId
, which is longer.
%dw 2.0
var firstInput = [
{ "bookId":"101",
"title":"world history",
"price":"19.99"
},
{
"bookId":"202",
"title":"the great outdoors",
"price":"15.99"
}
]
var secondInput = [
{
"bookId":"101",
"author":"john doe"
},
{
"bookId":"202",
"author":"jane doe"
}
]
output application/json
---
firstInput map (firstInputValue) -> using (id = firstInputValue.bookId)
{
theValue : id as Number,
theTitle: firstInputValue.title,
thePrice: firstInputValue.price as Number,
(secondInput filter ($.*bookId contains id) map (secondInputValue) -> {
theAuthor : secondInputValue.author
})
}
[
{
"theValue": 101,
"theTitle": "world history",
"thePrice": 19.99,
"theAuthor": "john doe"
},
{
"theValue": 202,
"theTitle": "the great outdoors",
"thePrice": 15.99,
"theAuthor": "jane doe"
}
]
Both scripts produce the same output.