🚀 DIGITAL TECH eBOOKS

Learn. Practice. Build.

Practical eBooks, interview questions, real-world projects and free learning resources for developers.

✓ Practical Content    ✓ Interview Focused    ✓ Real-World Examples

DataWeave Functions You'll Use Every Day in MuleSoft

DataWeave Functions You'll Use Every Day in MuleSoft

DataWeave is one of the most important skills for every MuleSoft developer. Whether you are transforming JSON, XML, CSV or database results, DataWeave functions make complex transformations much easier to implement.

In this post, let's look at some of the most commonly used DataWeave functions with simple, practical examples.






🔹 1. map()

The map() function is commonly used to transform every element of an array.

%dw 2.0
output application/json
---
payload map (item) -> {
  name: item.name,
  salary: item.salary
}

Input

[
  {
    "name": "John Doe",
    "salary": 50000
  },
  {
    "name": "Alex",
    "salary": 60000
  }
]

Output

[
  {
    "name": "John Doe",
    "salary": 50000
  },
  {
    "name": "Alex",
    "salary": 60000
  }
]

🔹 2. filter()

Use filter() when you want to keep only elements that satisfy a condition.

%dw 2.0
output application/json
---
payload filter (item) -> item.salary > 50000

This returns only employees whose salary is greater than 50,000.


🔹 3. filterObject()

filterObject() is useful when working with objects and you need to retain selected key-value pairs.

%dw 2.0
output application/json
---
payload filterObject ((value, key) -> key != "password")

This can be useful when removing sensitive fields from an object before sending data to another system.


🔹 4. pluck()

The pluck() function converts object values into an array and is useful when you need to iterate over object entries.

%dw 2.0
output application/json
---
payload pluck ((value, key) -> {
  field: key,
  value: value
})

🔹 5. distinctBy()

Use distinctBy() when you want to remove duplicate records based on a particular field.

%dw 2.0
output application/json
---
payload distinctBy $.department

This is particularly useful when processing duplicate records received from APIs or databases.


🔹 6. orderBy()

orderBy() can be used to sort an array based on a value.

%dw 2.0
output application/json
---
payload orderBy $.salary

The above example sorts employees according to salary.


🔹 7. groupBy()

groupBy() is extremely useful for grouping records according to a common field.

%dw 2.0
output application/json
---
payload groupBy $.department

For example, employees can be grouped into IT, HR, Finance and other departments.


🔹 8. sizeOf()

Use sizeOf() to determine the size of an array, string or other supported data type.

%dw 2.0
output application/json
---
{
  totalEmployees: sizeOf(payload)
}

🔹 9. upper()

The upper() function converts a string to uppercase.

%dw 2.0
output application/json
---
upper(payload.name)

For example:

"john doe" → "JOHN DOE"

🔹 10. lower()

The lower() function converts a string to lowercase.

%dw 2.0
output application/json
---
lower(payload.email)

🔹 11. contains()

Use contains() when you need to check whether a string contains a particular value.

%dw 2.0
output application/json
---
payload.name contains "John"

🔹 12. replace()

The replace() function is useful for modifying strings.

%dw 2.0
output application/json
---
payload.name replace "John" with "Alex"

🔹 13. ++

The ++ operator can be used to concatenate arrays, strings and objects depending on the data type.

%dw 2.0
output application/json
---
payload.firstName ++ " " ++ payload.lastName

🔹 14. default

The default operator is useful when a value may be missing or null and you want to provide a fallback value.

%dw 2.0
output application/json
---
{
  name: payload.name default "Unknown"
}

🏢 Real-World MuleSoft Example

Suppose an API returns a list of employees. We need to:

  • Remove inactive employees
  • Sort employees by salary
  • Return only selected fields

We can combine multiple DataWeave operations:

%dw 2.0
output application/json
---
(payload
    filter $.active == true
    orderBy $.salary
)
map {
    employeeId: $.id,
    employeeName: $.name,
    salary: $.salary
}

This demonstrates how DataWeave functions can be combined to build production-style transformations.


🎯 MuleSoft Interview Question

What is the difference between map(), filter(), mapObject() and filterObject() in DataWeave?

  • map() — transforms elements of an array.
  • filter() — selects elements from an array.
  • mapObject() — transforms key-value pairs of an object.
  • filterObject() — selects key-value pairs from an object.

💡 Best Practice

Don't try to solve every transformation using one huge DataWeave expression. Break complex transformations into understandable steps and choose functions based on the data structure you are processing.

The most important thing is to understand whether your input is an Array, Object, String, or another data type before selecting the appropriate function.


📚 Want to Practice More DataWeave?

Practice DataWeave with 1000 real-world problems with solutions, covering beginner to advanced transformations.

👉 Get 1000 Problems to DataWeave Mastery →


Follow Digital Tech eBooks for more MuleSoft, DataWeave, API-led connectivity, connectors, deployment and real-world integration tutorials.

Post a Comment

0 Comments