What Is groupBy() in DataWeave? Real-World MuleSoft Example
When working with MuleSoft integrations, you will often receive a large
collection of records that needs to be organized into groups.
DataWeave provides the groupBy() function to make this
transformation simple and readable.
In this practical example, we will group employee records by department using DataWeave 2.0.
🔹 What Does groupBy() Do?
The groupBy() function groups elements of an array based on
a value returned by the expression you provide.
For example, if employees belong to IT, HR and Finance, we can use
groupBy() to create separate groups for each department.
📥 Input JSON
[
{
"id": 101,
"name": "John Doe",
"department": "IT",
"salary": 60000
},
{
"id": 102,
"name": "Alex",
"department": "HR",
"salary": 55000
},
{
"id": 103,
"name": "Sam",
"department": "IT",
"salary": 70000
},
{
"id": 104,
"name": "David",
"department": "Finance",
"salary": 65000
},
{
"id": 105,
"name": "Emma",
"department": "HR",
"salary": 58000
}
]
💻 DataWeave 2.0 Solution
%dw 2.0
output application/json
---
payload groupBy $.department
That's it. DataWeave uses the value of department as the
grouping key.
📤 Output
{
"IT": [
{
"id": 101,
"name": "John Doe",
"department": "IT",
"salary": 60000
},
{
"id": 103,
"name": "Sam",
"department": "IT",
"salary": 70000
}
],
"HR": [
{
"id": 102,
"name": "Alex",
"department": "HR",
"salary": 55000
},
{
"id": 105,
"name": "Emma",
"department": "HR",
"salary": 58000
}
],
"Finance": [
{
"id": 104,
"name": "David",
"department": "Finance",
"salary": 65000
}
]
}
🔍 How Does It Work?
Step 1 — Start with the Array
The input is an array containing multiple employee objects.
Step 2 — Specify the Grouping Field
groupBy $.department
DataWeave evaluates $.department for every employee.
Step 3 — Create Groups
Employees having the same department value are placed into the same group.
Therefore:
- IT employees → IT group
- HR employees → HR group
- Finance employees → Finance group
🏢 Real-World MuleSoft Use Case
Imagine a MuleSoft API receives thousands of orders from an e-commerce platform.
Each order contains a region:
[
{
"orderId": "ORD001",
"region": "US",
"amount": 500
},
{
"orderId": "ORD002",
"region": "EU",
"amount": 700
},
{
"orderId": "ORD003",
"region": "US",
"amount": 300
}
]
We can group the orders by region:
%dw 2.0
output application/json
---
payload groupBy $.region
This can make downstream processing easier because records are already organized according to the required business dimension.
🔹 groupBy() with a Calculated Key
The grouping expression does not have to be a simple field reference. You can calculate the grouping key.
For example, employees can be grouped according to salary level:
%dw 2.0
output application/json
---
payload groupBy (
if ($.salary >= 70000)
"HIGH"
else
"STANDARD"
)
Here, the grouping key is calculated dynamically.
🔹 groupBy() vs filter()
| Function | Purpose |
|---|---|
| groupBy() | Organizes array elements into groups based on a key. |
| filter() | Selects only elements that satisfy a condition. |
For example:
groupBy → "Put records into groups"
filter → "Keep only matching records"
🎯 MuleSoft Interview Question
What is the purpose of groupBy() in DataWeave?
Answer:
groupBy() groups the elements of an array according to a
grouping expression and returns an object whose keys represent the
generated groups.
⚠️ Common Mistake
A common mistake is expecting groupBy() to return another
array.
When grouping an array, the result is organized using keys representing the groups.
Input:
Array
↓
groupBy()
↓
Grouped Object
💡 Production Tip
Before using groupBy() on very large datasets, consider the
size of the resulting grouped structure and the memory implications of
your transformation.
For large enterprise integrations, streaming, batching and appropriate processing strategies may be more suitable than building a very large in-memory grouped structure.
🚀 Final Takeaway
groupBy() is one of the most useful DataWeave functions for
organizing collections based on a business key.
Remember the simple pattern:
Array + groupBy(key) → Groups of related records
Mastering functions such as groupBy(),
map(), filter() and
distinctBy() will significantly improve your DataWeave
transformation skills.
📚 Want to Practice More DataWeave?
Practice DataWeave with 1000 real-world problems with complete solutions, from beginner concepts to advanced enterprise transformations.
Follow Digital Tech eBooks for more MuleSoft, DataWeave, API-led connectivity, connectors, deployment and real-world integration tutorials.

0 Comments