DataWeave Functions: Learn From Basics and Simplicity (Dev’s Choice)
In this article, learn some basic and useful functions of DataWeave 2.0 (Developer's choice) in MuleSoft for integrations.
Join the DZone community and get the full member experience.
Join For FreeHi Muleys!
In this post, we will be learning about basic and useful functions of DataWeave 2.0 with quick examples.
The list of functions used in the article below is selected out from the list of huge functions available in DataWeave(Developer's choice):
- Join (
join
) - Left Join (
leftJoin
) - Outer Join (
outerJoin
) - Nested join with
map
operator - Update as Function
- Update as Operator
- Max By (
maxBy
) - Min By (
minBy
) - Filtering an array (
filter
) - Map an array (
map
) - DistinctBy an array (
distinctBy
) - GroupBy an array (
groupBy
) - Reduce an array (
reduce
) - Flatten an array (
flatten
)
We may or may not have used the DataWeave function in our daily integrations. Let's see the examples below for each function.
1. Join
- The
join
function behaves similarly to a SQL database JOIN. - The
join
function combines elements of two arrays by matching two ID criteria for the same index in both arrays. - The left and right arrays must be arrays of objects.
- Importing
core::Arrays
function is required. - Ignores unmatched objects
DataWeave Code
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
//join(a, b, (emp) -> emp."Billing Country", (loc)-> loc."Billing Country")
//join(a,b, (a)-> a."Billing Country", (b)-> b."Billing Country")
join(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")
Output:
[
{
"l": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
},
"r": {
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
}
]
2. Left Join
- All the joined objects are returned.
- Importing
core::Arrays
function is required. - Any unmatched left elements are also added.
DataWeave Code
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
leftJoin(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")
Output:
[
{
"l": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
},
"r": {
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
},
{
"l": {
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account",
"Allowance": 2000
}
}
]
3. Outer Join
- All the joined objects are returned.
- Importing the
core::Arrays
function is required. - Any unmatched left element or right elements are also added.
DataWeave Code
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
outerJoin(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country")
Output:
[
{
"l": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
},
"r": {
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
},
{
"l": {
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account",
"Allowance": 2000
}
}
]
4. Nested Join With Map Operator
- Use the
map
function to iterate over each joined object. - Importing the
core::Arrays
function is required.
DataWeave Code
%dw 2.0
output application/json
import * from dw::core::Arrays
var a = [{"Name": "Ram","Billing City":"BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City":"HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
---
(join(a,b, (x)-> x."Billing Country", (y)-> y."Billing Country"))
map {
"info": $.l ++ $.r - "Billing Country"
}
Output:
[
{
"info": {
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000,
"Name": "Shyam",
"Billing City": "HYD",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 3000
}
}
]
5. Update as Function
For Fieldname
- This update function updates a field in an object with the specified string value.
- The function returns a new object with the specified field and value.
- Introduced in DataWeave version 2.2.2
- Importing the
util::Values
function is required.
DataWeave Code
%dw 2.0
output application/json
import * from dw::core::Arrays
import * from dw::util::Values
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
c update "element" with "abc" //string
Output:
{
"element": "abc"
}
For Index
- Updates an array index with the specified value
- This update function returns a new array that changes the value of the specified index.
- Introduced in DataWeave version 2.2.2
- Importing the
util::Values
function is required.
DataWeave Code
%dw 2.0
output application/json
import * from dw::core::Arrays
import * from dw::util::Values
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
d update 2 with 5 //index
Output:
[
1,
true,
5,
3,
false
]
6. Update as Operator
- This new update operator will update a specific field value with a new value given.
- This feature adds an easy way to update single values in nested data structures without requiring to understand functional recursion.
- No extra dw libraries are required.
DataWeave Code
%dw 2.0
output application/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
var d = [1, true, 2, 3, false]
---
c update {
case element at .element -> if (element == a) "Max" else "Mule"
}
Output:
{
"element": "Max"
}
7. Max By
- Iterates over an array and returns the highest value of comparable elements from it.
- The items must be of the same type.
maxBy
throws an error if they are not, and the function returns null if the array is empty.
DataWeave Code
%dw 2.0
output application/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
a maxBy $.Allowance
Output:
{
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account",
"Allowance": 2000
}
8. Min By
- Iterates over an array to return the lowest value of comparable elements from it.
- The items need to be of the same type.
minBy
returns an error if they are not, and it returns null when the array is empty.
DataWeave Code
%dw 2.0
output application/json
var a = [{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000}]
var b = [{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000}]
fun mapping() = (if (isEmpty(a)) b else a)
var c = {"element": mapping()}
---
a minBy $.Allowance
Output:
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account",
"Allowance": 1000
}
Input payload (common for all functions below)
[{"Name": "Ram","Billing City": "BLR","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 1000},
{"Name": "Max","Billing City": "NY","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 2000},
{"Name": "Shyam","Billing City": "HYD","Billing Country": "India","Message": "Hello world!","Type": "Account","Allowance": 3000},
{"Name": "John","Billing City": "FL","Billing Country": "USA","Message": "Hello world!!","Type": "Account","Allowance": 4000}]
9. Filtering an Array (filter)
To filter the data based on the condition.
DataWeave Code
%dw 2.0
output application/json
---
payload filter ((item, index) ->
item."Billing Country" == "India"
)
Output:
[
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
},
{
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
}
]
10. Map an Array (map)
- Transforming every item in an array
DataWeave Code
%dw 2.0
output application/json
---
payload map ((item, index) ->
{"Cities": if (item."Billing Country" == "USA") "USA" else "Others"}
)
Output:
[
{
"Cities": "Others"
},
{
"Cities": "USA"
},
{
"Cities": "Others"
},
{
"Cities": "USA"
}
]
11. DistinctBy an Array (distinctBy)
Remove duplicate items from an Array.
DataWeave Code
%dw 2.0
output application/json
---
payload distinctBy ((item, index) ->
item."Billing Country"
)
Output:
[
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
},
{
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account"
}
]
12. GroupBy an Array (groupBy)
- Grouping together items in an array based on some value
DataWeave Code
%dw 2.0
output application/json
---
payload groupBy ((item, index) ->
item."Billing Country"
)
Output:
{
"India": [
{
"Name": "Ram",
"Billing City": "BLR",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
},
{
"Name": "Shyam",
"Billing City": "HYD",
"Billing Country": "India",
"Message": "Hello world!",
"Type": "Account"
}
],
"USA": [
{
"Name": "Max",
"Billing City": "NY",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account"
},
{
"Name": "John",
"Billing City": "FL",
"Billing Country": "USA",
"Message": "Hello world!!",
"Type": "Account"
}
]
}
13. Reduce an Array (reduce)
- It can be used to transform an array to any other type.
DataWeave Code
%dw 2.0
output application/json
---
payload."Allowance" reduce ((item, accumulator) -> (item + accumulator))
Output:
10000
14. Flatten an Array (flatten)
DataWeave Code
%dw 2.0
output application/json
---
flatten (payload.Name)
Output:
[
"Ram",
"Max",
"Shyam",
"John"
]
Conclusion
As MuleSoft Developers, we use DataWeave codes almost daily in our integrations. The functions mentioned above of Array
and examples could help us achieve our desired outputs/results easily.
Happy learning!
Published at DZone with permission of Nitish Jain. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments