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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Exploring Operator, OpenAI’s New AI Agent
  • Building a Sample Kubernetes Operator on Minikube: A Step-by-Step Guide
  • How to Get Plain Text From Common Documents in Java
  • Low Code AI Agent Using Kumologica and Anthropic AI for Customer Feedback Sentiment Analysis

Trending

  • Navigating Double and Triple Extortion Tactics
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving

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
Likes (3)
Comment
Save
Tweet
Share
9.8K 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

  • Exploring Operator, OpenAI’s New AI Agent
  • Building a Sample Kubernetes Operator on Minikube: A Step-by-Step Guide
  • How to Get Plain Text From Common Documents in Java
  • Low Code AI Agent Using Kumologica and Anthropic AI for Customer Feedback Sentiment Analysis

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!