DataWeave 2.0: map vs filter vs mapObject vs filterObject — What's the Difference?
One of the most common DataWeave interview questions is the difference
between map(), filter(), mapObject()
and filterObject().
The easiest way to understand them is to remember one important rule:
Array → map() / filter()
Object → mapObject() / filterObject()
🔹 1. map() — Transform an Array
Use map() when you want to process every element of an array
and produce a new array.
Input
[
{
"id": 101,
"name": "John Doe",
"department": "IT"
},
{
"id": 102,
"name": "Alex",
"department": "HR"
}
]
DataWeave
%dw 2.0
output application/json
---
payload map (employee) -> {
employeeId: employee.id,
employeeName: employee.name
}
Output
[
{
"employeeId": 101,
"employeeName": "John Doe"
},
{
"employeeId": 102,
"employeeName": "Alex"
}
]
Remember: map() transforms every array element.
🔹 2. filter() — Select Array Elements
Use filter() when you want to keep only the array elements
that satisfy a condition.
Input
[
{
"name": "John Doe",
"salary": 50000
},
{
"name": "Alex",
"salary": 75000
},
{
"name": "Sam",
"salary": 45000
}
]
DataWeave
%dw 2.0
output application/json
---
payload filter (employee) -> employee.salary > 50000
Output
[
{
"name": "Alex",
"salary": 75000
}
]
Remember: filter() selects elements; it does
not transform every element into a different structure.
🔹 3. mapObject() — Transform an Object
mapObject() is used when you want to iterate over the
key-value pairs of an object and create a new object.
Input
{
"firstName": "John",
"lastName": "Doe",
"department": "IT"
}
DataWeave
%dw 2.0
output application/json
---
payload mapObject ((value, key) -> {
(upper(key)): value
})
Output
{
"FIRSTNAME": "John",
"LASTNAME": "Doe",
"DEPARTMENT": "IT"
}
Here, mapObject() processes each key-value pair of the
object.
🔹 4. filterObject() — Select Object Fields
Use filterObject() when you want to retain only specific
key-value pairs from an object.
Input
{
"name": "John Doe",
"email": "john@example.com",
"password": "secret123",
"department": "IT"
}
DataWeave
%dw 2.0
output application/json
---
payload filterObject ((value, key) -> key != "password")
Output
{
"name": "John Doe",
"email": "john@example.com",
"department": "IT"
}
This is useful when removing unwanted or sensitive fields before sending data to another system.
📊 Quick Comparison
| Function | Works On | Purpose |
|---|---|---|
| map() | Array | Transform each element |
| filter() | Array | Select elements |
| mapObject() | Object | Transform key-value pairs |
| filterObject() | Object | Select key-value pairs |
🧠 Easy Way to Remember
ARRAY
➡️ map() = Transform
➡️ filter() = Select
OBJECT
➡️ mapObject() = Transform
➡️ filterObject() = Select
🏢 Real-World MuleSoft Example
Imagine a MuleSoft API receives employee information and needs to prepare the data for a downstream system.
The requirement is:
- Keep only active employees.
- Transform the employee structure.
- Remove unnecessary fields.
A typical transformation could combine filtering and mapping:
%dw 2.0
output application/json
---
payload
filter $.active == true
map {
id: $.id,
name: $.name,
department: $.department
}
This demonstrates a very common DataWeave pattern: filter first, then transform.
🎯 MuleSoft Interview Question
What is the difference between map, filter, mapObject and filterObject in DataWeave?
Answer:
map() and filter() primarily operate on arrays,
while mapObject() and filterObject() operate on
objects.
map()→ transforms array elements.filter()→ selects array elements.mapObject()→ transforms object key-value pairs.filterObject()→ selects object key-value pairs.
⚠️ Common Mistake
A common beginner mistake is trying to use map() when the
input is an object, or using mapObject() when the input is
an array.
Before choosing a function, first identify the structure of your input:
Array → map() / filter()
Object → mapObject() / filterObject()
🚀 Final Takeaway
Understanding these four functions will help you solve a large number of real-world DataWeave transformations and MuleSoft interview questions.
The key is simple: map transforms, filter selects, and the Object versions work with key-value pairs.
📚 Want to Practice More DataWeave?
Build your DataWeave skills with 1000 practical 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