DataWeave Functions in MuleSoft: Essential Functions You'll Use Every Day
DataWeave is the powerful transformation language used in MuleSoft to transform, filter, map, and manipulate data.
Whether you are a beginner or preparing for a MuleSoft interview, understanding commonly used DataWeave functions is essential.
In this guide, we will cover some of the most useful DataWeave functions with simple examples.
1. map
The map function transforms every element of an array.
Input
[
10, 20, 30
]
DataWeave
%dw 2.0
output application/json
---
payload map (item) -> item * 2
Output
[
20,
40,
60
]
2. filter
filter is used to select elements that satisfy a condition.
%dw 2.0
output application/json
---
payload filter (item) -> item > 20
Output
[
30
]
3. mapObject
mapObject is used to transform the key-value pairs of an object.
%dw 2.0
output application/json
---
{
name: "John",
age: 30
} mapObject (value, key) -> {
(upper(key)): value
}
4. filterObject
filterObject filters the fields of an object based on a condition.
%dw 2.0
output application/json
---
payload filterObject (value, key) -> value != null
This is especially useful when removing unwanted null fields.
5. pluck
pluck converts object values into an array.
%dw 2.0
output application/json
---
{
name: "John",
age: 30
} pluck $
Output
[
"John",
30
]
6. groupBy
groupBy groups array elements based on a condition.
%dw 2.0
output application/json
---
payload groupBy $.department
For example, employees can be grouped by IT, HR, Finance, or Sales. This function is frequently used in real-world integration projects.
7. orderBy
orderBy sorts data.
%dw 2.0
output application/json
---
payload orderBy $.salary
You can use it when records need to be sorted by salary, date, name, or another field.
8. distinctBy
distinctBy removes duplicate records based on a selected field.
%dw 2.0
output application/json
---
payload distinctBy $.id
This is useful when processing duplicate customer, employee, or order records.
9. flatten
flatten converts nested arrays into a single array.
%dw 2.0
output application/json
---
flatten [[1, 2], [3, 4]]
Output
[
1,
2,
3,
4
]
10. sizeOf
sizeOf returns the size of an array, string, or other supported value.
%dw 2.0
output application/json
---
sizeOf(payload)
This is commonly used for validation and conditional processing.
11. upper
Converts text to uppercase.
%dw 2.0
output application/json
---
upper("mulesoft")
Output
MULESOFT
12. lower
Converts text to lowercase.
%dw 2.0
output application/json
---
lower("MULESOFT")
Output
mulesoft
13. trim
Removes unnecessary whitespace.
%dw 2.0
output application/json
---
trim(" MuleSoft ")
Output
MuleSoft
14. replace
Used to replace part of a string.
%dw 2.0
output application/json
---
"Hello MuleSoft" replace "MuleSoft" with "Developer"
Output
Hello Developer
15. substring
Extracts part of a string.
%dw 2.0
output application/json
---
substring("MuleSoft", 0, 4)
Output
Mule
16. isEmpty
Checks whether a value is empty.
%dw 2.0
output application/json
---
isEmpty(payload)
This is commonly used for validation before processing data.
17. default
Provides a fallback value when the original value is null or absent.
%dw 2.0
output application/json
---
{
name: payload.name default "Unknown"
}
If name is missing, the result will be:
{
"name": "Unknown"
}
18. if / else
Conditional logic is one of the most commonly used DataWeave techniques.
%dw 2.0
output application/json
---
{
status:
if (payload.amount > 1000)
"HIGH"
else
"LOW"
}
19. contains
Checks whether a string contains a specific value.
%dw 2.0
output application/json
---
"MuleSoft Developer" contains "MuleSoft"
Output
true
20. now
Returns the current date and time.
%dw 2.0
output application/json
---
{
processedAt: now()
}
This is useful for adding timestamps to integration messages.
Real-World Example
Suppose MuleSoft receives the following employee data:
[
{
"name": "John",
"department": "IT",
"salary": 90000
},
{
"name": "Alex",
"department": "HR",
"salary": 70000
},
{
"name": "David",
"department": "IT",
"salary": 95000
}
]
We can filter IT employees and return only their names:
%dw 2.0
output application/json
---
payload
filter $.department == "IT"
map $.name
Output
[
"John",
"David"
]
This simple example demonstrates how multiple DataWeave functions can be combined to solve a real integration requirement.
Key DataWeave Functions to Remember
| Function | Purpose |
|---|---|
map | Transform array elements |
filter | Select array elements |
mapObject | Transform object fields |
filterObject | Filter object fields |
pluck | Convert object values to array |
groupBy | Group records |
orderBy | Sort records |
distinctBy | Remove duplicates |
flatten | Flatten nested arrays |
sizeOf | Find size |
upper | Convert to uppercase |
lower | Convert to lowercase |
trim | Remove whitespace |
replace | Replace text |
substring | Extract text |
isEmpty | Check empty values |
default | Provide fallback values |
contains | Check for a value |
now | Get current timestamp |
Final Thoughts
Mastering DataWeave functions is one of the most important steps for becoming a strong MuleSoft developer.
Start with commonly used functions such as map, filter, mapObject, groupBy, distinctBy, flatten, default, and orderBy, then gradually move toward more advanced transformations.
The best way to learn DataWeave is through practice and real-world problems.
📚 Want to Practice More?
1000 Problems to DataWeave Mastery — Complete Beginner-to-Expert Bundle
Get 1000 practical DataWeave problems with solutions, designed to take you from beginner to advanced level.

0 Comments