Transforming messages with DataWeave

Published on
April 22, 2021
Author
MuleSoft Integration Team
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

Designing for Reality: Integrating 837 Claims When X12 Meets Production
BlogFeb 11, 2026

Designing for Reality: Integrating 837 Claims When X12 Meets Production

Designing 837 Claim Integration for Real-World Healthcare Systems When it comes to 837 claim integration, most architects assume the X12 specification guarantees predictability. On paper, the 837 Professional, Institutional, and Dental transactions look clean and orderly. In production? Not even close. Real-world 837 files behave differently across trading partners. Loops appear conditionally. Repeatable segments shift… Continue reading Designing for Reality: Integrating 837 Claims When X12 Meets Production

Read More
Blog
5 min read

Designing for Reality: Integrating 837 Claims When X12 Meets Production

Designing 837 Claim Integration for Real-World Healthcare Systems When it comes to 837 claim integration, most architects assume the X12 specification guarantees predictability. On paper, the 837 Professional, Institutional, and Dental transactions look clean and orderly. In production? Not even close. Real-world 837 files behave differently across trading partners. Loops appear conditionally. Repeatable segments shift… Continue reading Designing for Reality: Integrating 837 Claims When X12 Meets Production

Read More
AI-Driven PDF Parsing in Salesforce
BlogDec 4, 2025

AI-Driven PDF Parsing in Salesforce

Introduction For the current digital ecosystem, data is an important aspect for decision-making. Yet, for many organizations, a significant portion of this valuable data remains locked away in unstructured formats. Organizations handle thousands of PDF documents daily — ranging from contracts and invoices to lab reports, quotations, and service agreements. Traditionally, extracting structured data from… Continue reading AI-Driven PDF Parsing in Salesforce

Read More
Blog
6 min read

AI-Driven PDF Parsing in Salesforce

Introduction For the current digital ecosystem, data is an important aspect for decision-making. Yet, for many organizations, a significant portion of this valuable data remains locked away in unstructured formats. Organizations handle thousands of PDF documents daily — ranging from contracts and invoices to lab reports, quotations, and service agreements. Traditionally, extracting structured data from… Continue reading AI-Driven PDF Parsing in Salesforce

Read More
Compression Namespace in Apex: A Powerful New Salesforce Feature
BlogNov 5, 2025

Compression Namespace in Apex: A Powerful New Salesforce Feature

Introduction Working with documents inside Salesforce has always challenged developers because of the platform’s multitenant constraints. Previously, packaging and sending files in a compact form required external services, like an AWS Lambda function, that retrieved files via API and then compressed them. With the introduction of the Compression Namespace and the powerful pre-defined Apex functions,… Continue reading Compression Namespace in Apex: A Powerful New Salesforce Feature

Read More
Blog
5 min read

Compression Namespace in Apex: A Powerful New Salesforce Feature

Introduction Working with documents inside Salesforce has always challenged developers because of the platform’s multitenant constraints. Previously, packaging and sending files in a compact form required external services, like an AWS Lambda function, that retrieved files via API and then compressed them. With the introduction of the Compression Namespace and the powerful pre-defined Apex functions,… Continue reading Compression Namespace in Apex: A Powerful New Salesforce Feature

Read More
Boost LWC Performance with Debouncing
BlogSep 18, 2025

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

Read More
Blog
7 min read

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

Read More