MuleSoft Technical Guides

Transforming messages with DataWeave

User MuleSoft Integration Team
Calendar April 22, 2021

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.

 

 

 

 

 

 

Leave a comment

Your email address will not be published. Required fields are marked *