Deep Dive Into DataWeave Core Arrays Module: Part 1
In this article, we will be using a JSON payload for understanding various functions with DW Core Arrays.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
DataWeave Core Arrays module provides the helper function which can be used on the array of items. There are many functions provided by the DW Core Array module like sumBy, countBy, some, every, etc.
In this article, we will be using the below JSON payload for understanding various functions with DW Core Arrays.
[
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"age":25,
"salary":20000
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"age":28,
"salary":10000
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"age":32,
"salary":30000
},
{
"id": "4",
"firstName": "Kevin",
"lastName": "Peter",
"age":35,
"salary":40000
},
{
"id": "5",
"firstName": "Jackie",
"lastName": "Chen",
"age":40,
"salary":50000
}
]
Example 1
We will use some function to check whether list having
- At least one employee salary less than 40000
- At least one employee salary greater than 50000
some returns true
if at least one element in the array matches the specified condition.
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
employeeSalaryLessThan40000: payload..salary some ($ < 40000), //syntax 1
employeeSalaryGreaterThan50000: some(payload..salary, (value)->(value > 50000)) //syntax 2
}
Output
xxxxxxxxxx
{
"employeeSalaryLessThan40000": true,
"employeeSalaryGreaterThan50000": false
}
Example 2
We will use every function to check whether list having
- At least all employees salary less than or equal to 50000.
- At least all employees salary greater than 50000.
every returns true
if every element in the array matches the condition.
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
employeeSalaryLessThanorEqual50000: payload..salary every ($ <= 50000), //syntax 1
employeeSalaryGreaterThan50000: every(payload..salary, (value)->(value > 50000)) //syntax 2
}
Output
xxxxxxxxxx
{
"employeeSalaryLessThanorEqual50000": true,
"employeeSalaryGreaterThan50000": false
}
Example 3
We will use countBy function to check whether list having
- Count employee whose salary less than or equal to 30000.
- Count employee whose salary greater than 40000.
countBy counts the elements in an array that match the results of a function.
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
countEmployeeSalaryLessThanorEqual30000: payload..salary countBy ($ <= 30000), //syntax 1
countEmployeeSalaryGreaterThan40000: countBy(payload..salary, (value)->(value > 40000)) //syntax 2
}
Output
xxxxxxxxxx
{
"countEmployeeSalaryLessThanorEqual30000": 3,
"countEmployeeSalaryGreaterThan40000": 1
}
Example 4
We will use sumBy function to check whether list having
- Sum employee count whose salary less than or equal to 30000.
- Sum employee count whose salary greater than 40000.
sumBy returns the sum of the values of the elements in an array.
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
---
{
sumEmployeeSalaryLessThanorEqual30000: payload..salary sumBy (if($ <= 30000) $ else 0), //syntax 1
sumEmployeeSalaryGreaterThan40000: sumBy(payload..salary, (value)->(if(value > 40000) value else 0)) //syntax 2
}
Output
xxxxxxxxxx
{
"sumEmployeeSalaryLessThanorEqual30000": 60000,
"sumEmployeeSalaryGreaterThan40000": 50000
}
Now, you know DataWeave Core Arrays Module helper functions.
Opinions expressed by DZone contributors are their own.
Comments