Mule 4 + MuleSoft + DataWeave 2.3.0 New Features + Example

  • In this article we will discuss Dataweave 2.3.0 New Features in detail with working examples.
  • Dataweave 2.3.0 works with mule runtime 4.3.0 or later version.

Dataweave 2.3.0 New Features

In this blog we will discuss following features in detail.

DataWeave
  • Update Operator
  • Conditional Update
  • namesOf() function
  • valuesOf() function
  • withMaxSize function
  • upsert/insert new field using Update Operator

MuleSoft DataWeave Expression Language

The Mule 4.3.0 release supports DataWeave 2.3.0, which introduces a number of functions, types, and annotations that are not supported by Mule 4.1 and Mule 4.2.

DataWeave Expression Examples
What is Update Operator in Mule 4?

A new update operator introduced as part of Dataweave 2.3.0 in Mule 4.3.0. It is used to update the specific field with new value. If you remember earlier we used to go through all the key value pairs to update the values.

What is Conditional Update in Mule 4?

By using Update Operator we can update the value for a specific field with specific condition.

What is namesOf function in Mule 4?

This function is used to return array of string for all the keys inside a object.

What is valuesOf function in Mule 4?

This function is used to return array of string for all the values inside a object.

What is withMaxSize function in Mule 4?

In DataWeave withMaxSize function is used the get the string value on the basis of the length defined withMaxSize, if the defined length is less that the string value then it cuts the length from left to right as substring method in Java.

How Update Operator works in DataWeave expression

A new update operator introduced as part of Dataweave 2.3.0 in Mule 4.3.0.
It used to update the specific field with new value. If you do remember earlier we used to go through all the key value pairs to update the values. For example we can see Address1, Address2 and PostalCode updated with new values in following example.

Sample Input (Payload):

{
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2435 Fair Oaks Blvd",
  "ADDRESS2": "Space 201E",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
}

Set the variable as NewAddress with value “2439 Fair Oaks Blvd”, Write the Dataweave expression to update the values for few fields.

Dataweave Expression:

%dw 2.0
output application/json
---
payload update {
  case ADDRESS1 at .ADDRESS1 -> vars.NewAddress
  case ADDRESS2 at .ADDRESS2 -> vars.NewAddress
  case POSTALCODE at .POSTALCODE -> "92125"
}

Result (Output):

{
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2439 Fair Oaks Blvd",
  "ADDRESS2": "2439 Fair Oaks Blvd",
  "POSTALCODE": "92125",
  "CITY": "San Diego"
}

How Conditional Update Operator works in DataWeave expression

As we seen, by using Update Operator we can update the value for a specific field. Here, in this example we will apply some condition along with Update Operator as below.

Sample Input (Payload):

[
  {
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2435 Fair Oaks Blvd",
  "ADDRESS2": "Space 201E",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
  },
  {
  "CUSTOMER_ID": 11,
  "CUSTOMER_NAME": "San Petter",
  "ADDRESS1": "2488 Fair Oaks Blvd",
  "ADDRESS2": "Space 201M",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
}
]

Concatenate “California” with ADDRESS1 and ADDRESS2 on Conditional basis.

Dataweave Expression:

%dw 2.0
output application/json
---
payload map ((user) ->
  user update {
  case ADDRESS1 at .ADDRESS1 if(ADDRESS1 contains "2488") -> ADDRESS1 ++ " California"
  case ADDRESS2 at .ADDRESS2 if(ADDRESS2 contains "201M") -> ADDRESS2 ++ " California"
}
)

Result (Output):

[
  {
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2435 Fair Oaks Blvd California",
  "ADDRESS2": "Space 201E",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
  },
  {
  "CUSTOMER_ID": 11,
  "CUSTOMER_NAME": "San Petter",
  "ADDRESS1": "2488 Fair Oaks Blvd California",
  "ADDRESS2": "Space 201M",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
}
]

How does namesOf() function work in DataWeave expression

This function used to return array of string for all the keys inside a object. It works on the object and return array as below.

Sample Input (Payload):

{
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2435 Fair Oaks Blvd",
  "ADDRESS2": "Space 201E",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
}

Let’s apply DataWeave Expression on above payload.

Dataweave Expression:

%dw 2.0
output application/json
---
{
  "allkeys" : namesOf(payload)
}

Result (Output):

{
  "allkeys": [
    "CUSTOMER_ID",
    "CUSTOMER_NAME",
    "ADDRESS1",
    "ADDRESS2",
    "POSTALCODE",
    "CITY"
  ]
}

How does valuesOf() function work in DataWeave expression

This function used to return array of string for all the values inside a object. It works on the object and return array as below.

Sample Input (Payload):

{
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2435 Fair Oaks Blvd",
  "ADDRESS2": "Space 201E",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
}

Let’s apply DataWeave Expression on above payload.

Dataweave Expression:

%dw 2.0
output application/json
---
{
  "allvalues" : valuesOf(payload)
}

Result (Output):

{
  "allvalues": [
    10,
    "San Marcos",
    "2435 Fair Oaks Blvd",
    "Space 201E",
    "92122",
    "San Diego"
  ]
}

How we can use withMaxSize in DataWeave expression

In DataWeave withMaxSize function used the get the string value on the basis of the length defined withMaxSize.

Suppose, we defined the length is less that the string value, then it will cut the string length from left to right.

With below working example, we will see – how we can extract the string value for fields like CUSTOMER_NAME, ADDRESS1, ADDRESS2.

Sample Input (Payload):

[
  {
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2435 Fair Oaks Blvd",
  "ADDRESS2": "Space 201E",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
  },
  {
  "CUSTOMER_ID": 11,
  "CUSTOMER_NAME": "San Petter",
  "ADDRESS1": "2488 Fair Oaks Blvd",
  "ADDRESS2": "Space 201M",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
}
]

Dataweave Expression:

%dw 2.0
import withMaxSize from dw::core::Strings
output application/json
---
payload map () ->
{
  "CUSTOMER_ID":$.CUSTOMER_ID,
  "CUSTOMER_NAME":$.CUSTOMER_NAME withMaxSize 3,
  "ADDRESS1":$.ADDRESS1 withMaxSize 4,
  "ADDRESS2":$.ADDRESS2 withMaxSize 5
}

Result (Output):

[
  {
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San",
  "ADDRESS1": "2435",
  "ADDRESS2": "Space",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
  },
  {
  "CUSTOMER_ID": 11,
  "CUSTOMER_NAME": "San",
  "ADDRESS1": "2488",
  "ADDRESS2": "Space",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
 }
]

How we can insert new field using Update Operator in DataWeave expression

Update Operator enables to upsert/insert a field if it is not present by using the ! symbol at the end of the selector expression.

In below example we can see Email field is missing and we have inserted the Email field using Update operator using simple data weave expression as below.

Sample Input (Payload):

[
  {
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San Marcos",
  "ADDRESS1": "2435 Fair Oaks Blvd",
  "ADDRESS2": "Space 201E",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
  },
  {
  "CUSTOMER_ID": 11,
  "CUSTOMER_NAME": "San Petter",
  "ADDRESS1": "2488 Fair Oaks Blvd",
  "ADDRESS2": "Space 201M",
  "POSTALCODE": "92122",
  "CITY": "San Diego"
}
]

Dataweave Expression:

%dw 2.0
output application/json
---
payload map ((user) ->
  user update {
  case EMAIL at .EMAIL! -> if(EMAIL == null) "test@thebasictechinfo.com" else EMAIL

Result (Output):

[
  {
  "CUSTOMER_ID": 10,
  "CUSTOMER_NAME": "San",
  "ADDRESS1": "2435",
  "ADDRESS2": "Space",
  "POSTALCODE": "92122",
  "CITY": "San Diego",
  "EMAIL": "test@thebasictechinfo.com"
  },
  {
  "CUSTOMER_ID": 11,
  "CUSTOMER_NAME": "San",
  "ADDRESS1": "2488",
  "ADDRESS2": "Space",
  "POSTALCODE": "92122",
  "CITY": "San Diego",
  "EMAIL": "test@thebasictechinfo.com"
 }
]

Keep learning :). Have a great Day.