DW Complex Operators: DataWeave 2.0 Transformations
This article is about writing complex DataWeave codes using (some, every, countBy, sumBy) operators after importing DW core libraries.
Join the DZone community and get the full member experience.
Join For FreeHi Muleys,
This article is about writing complex DataWeave codes using (some
, every
, countBy
, sumBy
) operators after importing DW core libraries.
We all know how to write DataWeave coding but to use some complex operators like (some
, every
, countBy
, sumBy
, and more), we need to import DW libraries.
Example:
xxxxxxxxxx
import * from dw::core::Arrays
Let's begin with operators:
1. some
:
It is Boolean, returns either true or false.
Example:
DataWeave Code:
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
var a = [{name: "Max", email: "abc@.xyzcom", phone: "91 0808907890", company: "MuleSoft"},{name: "Mule", email: "abc@xyz.com", phone: "91 0808907890", company: "MuleSoft"}]
---
{
"Name": a.name some($ contains "M"),
"Company": a.company some($ matches "XYZ")
}
Expression Output:
xxxxxxxxxx
{
"Name": true,
"Company": false
}
2. every
:
It is Boolean, return true only if every element matches the condition else false.
Example:
DataWeave Code:
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
var a = [{name: "Max", email: "abc@.xyzcom", phone: "91 0808907890", company: "MuleSoft"},{name: "Mule", email: "abc@xyz.com", phone: "91 0808907890", company: "MuleSoft"}]
---
{
"Name": a.name every($ contains "S"),
"Company": a.company every($ contains "M")
}
Expression Output:
xxxxxxxxxx
{
"Name": false,
"Company": true
}
3. countBy
:
It returns the count of the elements as per the condition is applied.
Example:
DataWeave Code:
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
var a = [{name: "Max", email: "abc@.xyzcom", phone: "91 0808907890", company: "MuleSoft", allowance: 2000},{name: "Mule", email: "abc@xyz.com", phone: "91 0808907890", company: "MuleSoft",allowance: 4000}]
---
{
"Name": a.name countBy($ contains "M"),
"Company": a.company countBy($ contains "F"),
"Count": a.allowance countBy($>1000),
}
Expression Output:
xxxxxxxxxx
{
"Name": 2,
"Company": 0,
"Count": 2
}
4. sumBy
:
It returns the sum of total elements if the applied condition matches.
Example:
DataWeave Code:
xxxxxxxxxx
%dw 2.0
import * from dw::core::Arrays
output application/json
var a = [{name: "Max", email: "abc@.xyzcom", phone: "91 0808907890", company: "MuleSoft", allowance: 2000},{name: "Mule", email: "abc@xyz.com", phone: "91 0808907890", company: "MuleSoft",allowance: 4000}]
---
{
"Name": a.name sumBy(if($ contains "M") 1 else 0),
"Company": a.company sumBy(if($ contains "Z") 1 else 0),
"Sum": a.allowance sumBy(if($>1000) $ else 0),
}
Expression Output:
xxxxxxxxxx
{
"Name": 2,
"Company": 0,
"Sum": 6000
}
Conclusion
Using the above Operators and DW Libraries in DataWeave/Transform Message component, we can transform our data as per our complex requirements.
Happy Learning!!
Opinions expressed by DZone contributors are their own.
Comments