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

Related

  • DNS Propagation Doesn't Have to Take 24 Hours
  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • How to Verify Domain Ownership: A Technical Deep Dive
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails

Trending

  • 5 Layers of Prompt Injection Defense You Can Wire Into Any Node.js App
  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines

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
10.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: "[email protected]", phone: "91 0808907890", company: "MuleSoft"},{name: "Mule", email: "[email protected]", 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: "[email protected]", phone: "91 0808907890", company: "MuleSoft"},{name: "Mule", email: "[email protected]", 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: "[email protected]", phone: "91 0808907890", company: "MuleSoft", allowance: 2000},{name: "Mule", email: "[email protected]", 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: "[email protected]", phone: "91 0808907890", company: "MuleSoft", allowance: 2000},{name: "Mule", email: "[email protected]", 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

  • DNS Propagation Doesn't Have to Take 24 Hours
  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • How to Verify Domain Ownership: A Technical Deep Dive
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook