Transforming messages with DataWeave

DataWeave is an expression language designed by MuleSoft. The language helps in transforming the incoming payload to various payloads as per the requirements. Most developers write transformation script in the transform message or set payload components. The language is tightly integrated with the Mule runtime engine and is a must-learn for every MuleSoft developer.
A few examples for transforming the input payload into various patterns:
Example 1: Given an array, we’ll generate the following output pattern using the input payload.
Input payload:
[
{"name": "Roger" },
{"name": "Michael" },
{"name": "Harris" }
]
Required Output Pattern:
[
{
"user 1": "Roger"
},
{
"user 2": "Micheal"
},
{
"user 3": "Harris"
}
]
Expression Used to Transform:
%dw 2.0
output application/json
---
payload.name map ((item, index) ->
{
("user" ++" "++ index+1): item
})
Example 2: Given two arrays, [1,2,3,4,5,6,7] and [4,5,6,7,1], we’ll merge into a single array without duplicates, [1,2,3,4,5,6,7]:
Expression Used to Transform:
%dw 2.0 output application/json var str = [1,2,3,4,5,6,7] var str1= [4,5,6,7,1] --- (str ++ str1) distinctBy ((item, index) ->item )
Example 3: From the object, we’ll remove the “last name” and “first name” key-value pair.
Input Payload :
{
"first name": "Keith",
"last name": "Peters",
"age": 25
}
Required Output Pattern :
{
"age": 25
}
Expression Used to Transform:
%dw 2.0 output application/json --- payload filterObject ((value, key) -> (key as String != "first name") and key as String !="last name")
Example 4: From the array [1,2,3,’a’,’b’], remove 2 and b.
Expression Used to Transform:
%dw 2.0 output application/json var arr = [1,2,3,'a','b'] --- arr-2-'b'
Example 5: Given the below JSON, transform all names to uppercase.
Input Payload :
[
{"name": "Roger" },
{"name": "Michael" },
{"name": "Harris" }
]
Output Pattern:
[
{
"Name": "ROGER"
},
{
"Name": "MICHAEL"
},
{
"Name": "HARRIS"
}
]
Expression Used to Transform:
%dw 2.0 output application/json fun UpperCase(input_value)=upper(input_value) --- payload map(item, index) -> Name:UpperCase(item.name)
Example 6: Retrieve all the records of users with the age greater than 20.
Input Payload :
[
{
"name": "Chris Jordan",
"age": "19"
},
{
"name": "Glenn Maxwell",
"age": "24"
},
{
"name": "Mike",
"age": "20"
}
]
Expression Used to Transform:
%dw 2.0 output application/json --- payload filter ((item, index) -> item.age>20)
Example 7: Retrieve the average age of persons in the input payload.
Input Payload :
[
{
"name": "Chris Jordan",
"age": "19"
},
{
"name": "Glenn Maxwell",
"age": "24"
},
{
"name": "Mike",
"age": "20"
}
]
Expression Used to Transform:
%dw 2.0 output application/json --- payload filter ((item, index) -> item.age>20)
Example 8: Sort persons in payload according to age.
Input Payload:
[
{
"name": "Chris Jordan",
"age": "19"
},
{
"name": "Glenn Maxwell",
"age": "24"
},
{
"name": "Mike",
"age": "20"
}
]
Expression Used to Transform:
%dw 2.0 output application/json --- payload orderBy ($.age)
[ If we want the data in descending order]
%dw 2.0 output application/json --- payload orderBy (-$.age)
Example 9: Skip the null values from input payload
Input Payload :
[
{
"name": "Chris Jordan",
"age": null
},
{
"name": "Glenn Maxwell",
"age": "24"
},
{
"name": "Lisa",
"age": null
}
]
Expression Used to Transform:
%dw 2.0 output application/json skipNullOn = 'everywhere' --- payload
Example 10: Extract the file name from the input payload which is after “/”
{
"filename": "njnjnjnnjnbhbhjb/ui900jnnjn"
}
Expression Used to Transform:
%dw 2.0 output application/json import * from dw::core::Strings --- substringAfterLast(payload.filename,'/')
Example 11: Transform the present date and time to format like “yyyy-MM-dd HH:mm:ss.S”
Expression Used to Transform:
%dw 2.0
output application/json
---
now() as LocalDateTime as String {format: "yyyy-MM-dd HH:mm:ss.S"}
In all the above examples, we took the input payloads in JSON formats and tried to convert the inputs into the required patters using DataWeave.
Find more MuleSoft technical guides at Caelius Consulting Resource Centre.
Recent Blogs

Boost LWC Performance with Debouncing
Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing
Boost LWC Performance with Debouncing
Introduction Lightning Web Components (LWC) is a modern framework for building fast and dynamic user interfaces on the Salesforce platform. However, one common challenge in web development, including LWC, is efficiently handling user input, especially when dealing with rapid or repetitive events, such as typing in a search field. This is where debouncing becomes an… Continue reading Boost LWC Performance with Debouncing

Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers
Introduction In order to succeed in today’s fast-paced business landscape, precision and speed define competitive advantage. For businesses, especially those managing complex product catalogs, ensuring accurate pricing on sales orders or custom lines can be a time-consuming and error-prone task. To overcome this challenge, Salesforce trigger handlers offer a powerful solution to automate the entire… Continue reading Salesforce Pricing Automation: Boost Efficiency And Accuracy with Apex Triggers

Connecting MuleSoft and Azure SQL with Entra ID
Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID
Connecting MuleSoft and Azure SQL with Entra ID
Introduction Establishing a secure connection between MuleSoft and Azure SQL Database can be challenging, especially if you are using Entra ID (formerly known as Azure Active Directory) for authentication. This blog walks through a fully working configuration for connecting to Azure SQL using ActiveDirectoryServicePrincipal in Mule runtime 4.7.4 with Java 8 — addressing driver setup,… Continue reading Connecting MuleSoft and Azure SQL with Entra ID

Understanding Salesforce Flow Approval Processes
Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes
Understanding Salesforce Flow Approval Processes
Introduction: Salesforce introduced Flow Approval Processes in the Spring '25 release. This is an evolved version of the classic approval process model, powered by Flow Orchestrator. The new approach brings unprecedented flexibility, enabling the creation of dynamic, multi-level, and logic-driven approval workflows that are entirely declarative. Continue reading the blog to get a deeper understanding… Continue reading Understanding Salesforce Flow Approval Processes