Mulesoft DataWeave Transform Examples

To Transform the data, Mulesoft has provided the dataweave code. The DW code can be used in components like transform message, set payload, and other supported components. Mule 4 supports DW 2.0 and Mule 3 supports DW 1.0.

In this article, we will cover data transformations in Mule 4 only. Here, I have provided multiple examples that help in your day-to-day Mule activities.

Mule 4 DW 2.0 Transformation Examples

The following examples require data input. Here I am using XML and JSON inputs which are provided below:

--JSON Input
{
  "name": : "bigdaters",
  "country" : "United States"
}
--XML input 
<names>
  <name>bigdaters</name>
  <country>United States</country>
</names>

If the input is not specified in any example, by default it assumes the JSON input.

Convert XML to JSON

Transforming XML to JSON and vice versa is a common operation in the API world. In this example, we will see how to transform the xml input to json output. In the DW code, we have to specify the output header as application/json as shown in the below code.

%dw 2.0
output application/json
---
payload

This will convert to the following JSON format.

{
  "names": {
    "name": "bigdaters",
    "country": "United States"
  }
}

To get rid of the outer names field in the JSON output, then simply call payload.names as shown in the following code.

%dw 2.0
output application/json
---
payload.names

Transform JSON to XML

Unlike JSON, the XML dataset requires an outer closing tag. Otherwise, the conversion fails. The below code transforms the JSON input to XML output.

%dw 2.0
output application/xml
---
{
  "names" : payload
}

Here, we have used the application/xml output header and also the names outer tag. This will emit the below XML data.

<?xml version='1.0' encoding='UTF-8'?>
<names>
  <name>bigdaters</name>
  <country>United States</country>
</names>

Delete/Add a Field from the Payload

Removing and adding new fields in the input payload is one of the most common operations. In this example, we will remove the country field and add the state field.

%dw 2.0
output application/json
---
payload - "country" ++ {"state" : "Washington"}

This will produce the below output.

{
  "name": "bigdaters",
  "state": "Washington"
}

Replace Function in DW

The replace function can be used to replace a value with another value in a string data. The syntax of replace function is

inputString replace oldString with newString

The following code replaces the string User with World

%dw 2.0
output application/json
---
"Hello User" replace "User" with "World"

The output of this code is

"Hello World"

Convert String to Upper and Lower Case

To bring data into a standard format, we have to convert strings to either upper or lower case. The following DW code demonstrates the usage of upper and lower-case functions.

%dw 2.0
output application/json
---
{
    "name" : upper(payload.name),
    "country" : lower(payload.country)
}

The output of this code is

{
  "name": "BIGDATERS",
  "country": "united states"
}

Camelize Function

This is also one of the case transform functions. It converts the string into camel case format. It removes all the underscores and converts the first letter of the word next to the underscore to upper case.

%dw 1.0
output application/json
---
{
  a: camelize "customer_care"
}

This will give the below output.

{
  "a" : "customerCare"
}

Note: The Camelize function works only Mule 3 version.

Sum of Values in an Array

Fortunately DW, provides the sum function that finds the total sum of all values in an array without us iterating over the array and finding the sum.

%dw 2.0
output application/json
---
sum([1,4,3])

This outputs the value 8. See how easy to find the sum of array values.

Convert Delimitted String into Array

The splitBy operator tokenizes a delimitted string and produces an array. The usage of splitBy is illustrated in the below example.

%dw 2.0
output application/json
---
"abc:def:ghi" splitBy ":"

The output of this code execution is

[
  "abc",
  "def",
  "ghi"
]

Average of Array Values

Just like the sum function, the avg function finds the average of all values in the array. The usage of avg function is shown below.

%dw 2.0
output application/json
---
avg([1,2,3])

This will result in the output 2.

In the coming articles, we will see more examples of data transformation using DW code.