DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • Java String: A Complete Guide With Examples
  • The Power of ShardingSphere With Spring Boot
  • Error Handling Inside Kumologica Subflow

Trending

  • The Data Streaming Landscape 2024
  • 10 Tips To Improve Python Coding Skills in 2024
  • Implementing Persistence With Clean Architecture
  • Types of Bot Traffic on a Public Internet Website

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.

By 
Nitish Jain user avatar
Nitish Jain
·
Mar. 22, 21 · Code Snippet
Like (3)
Save
Tweet
Share
9.1K Views

Join the DZone community and get the full member experience.

Join For Free

Hi 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: 

Plain Text
 




xxxxxxxxxx
1


 
1
import * from dw::core::Arrays



Let's begin with operators:

1. some:

It is Boolean, returns either true or false.

Example:

DataWeave Code:

Plain Text
 




xxxxxxxxxx
1
17
9


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
var a = [{name: "Max", email: "abc@.xyzcom", phone: "91 0808907890", company: "MuleSoft"},{name: "Mule", email: "abc@xyz.com", phone: "91 0808907890", company: "MuleSoft"}]
5
---
6
{
7
"Name": a.name some($ contains "M"),
8
"Company": a.company some($ matches "XYZ")
9
}



Expression Output:

Plain Text
 




xxxxxxxxxx
1


 
1
{
2
  "Name": true,
3
  "Company": false
4
}



2. every:

It is Boolean, return true only if every element matches the condition else false.

Example:

DataWeave Code:

Plain Text
 




xxxxxxxxxx
1
17
9


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
var a = [{name: "Max", email: "abc@.xyzcom", phone: "91 0808907890", company: "MuleSoft"},{name: "Mule", email: "abc@xyz.com", phone: "91 0808907890", company: "MuleSoft"}]
5
---
6
{
7
"Name": a.name every($ contains "S"),
8
"Company": a.company every($ contains "M")
9
}



Expression Output:

Plain Text
 




xxxxxxxxxx
1


 
1
{
2
  "Name": false,
3
  "Company": true
4
}



3. countBy:

It returns the count of the elements as per the condition is applied.

Example:

DataWeave Code:

Plain Text
 




xxxxxxxxxx
1
19


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
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}]
5
---
6
{
7
"Name": a.name countBy($ contains "M"),
8
"Company": a.company countBy($ contains "F"),
9
"Count": a.allowance countBy($>1000),
10
}



Expression Output:

Plain Text
 




xxxxxxxxxx
1


 
1
{
2
  "Name": 2,
3
  "Company": 0,
4
  "Count": 2
5
}



4. sumBy:

It returns the sum of total elements if the applied condition matches.

Example:

DataWeave Code:

Plain Text
 




xxxxxxxxxx
1
19


 
1
%dw 2.0
2
import * from dw::core::Arrays
3
output application/json
4
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}]
5
---
6
{
7
"Name": a.name sumBy(if($ contains "M") 1 else 0),
8
"Company": a.company sumBy(if($ contains "Z") 1 else 0),
9
"Sum": a.allowance sumBy(if($>1000) $ else 0),
10
}



Expression Output:

Plain Text
 




xxxxxxxxxx
1
10
9


 
1
{
2
  "Name": 2,
3
  "Company": 0,
4
  "Sum": 6000
5
}



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!!

Plain text Operator (extension)

Opinions expressed by DZone contributors are their own.

Related

  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • Java String: A Complete Guide With Examples
  • The Power of ShardingSphere With Spring Boot
  • Error Handling Inside Kumologica Subflow

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: