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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Debugging and Performance Tuning in Pega Using PAL, Tracer, and Clipboard
  • DNS Propagation Doesn't Have to Take 24 Hours
  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)

Trending

  • Fix the Target, Precompute Once: A Backend-Free Word-Ladder Solver With a BFS Distance Field
  • How Kestra Unifies Enterprise Workflow Orchestration and Agentic AI
  • Part 3: End-to-End Tracing and Observability Across Goose, agentgateway, and Quarkus
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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
·
Mar. 22, 21 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
10.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Debugging and Performance Tuning in Pega Using PAL, Tracer, and Clipboard
  • DNS Propagation Doesn't Have to Take 24 Hours
  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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