DATAWEAVE 2.3.0 NEW FEATURES

 

In this tutorial we will demonstrate Dataweave 2.3.0 New Features, it is supported for mule runtime 4.3.0 or later.

 

Update Operator

A new update operator added as part of Dataweave 2.3.0 which is used to update the specific field with new value, earlier we used to go through all the key value pair to update the values, with below example we can see Address1,Address2 and PostalCode updated with new values

Sample Input:

{
  "CUSTOMER_ID": 1,
  "CUSTOMER_NAME": "TEST11",
  "ADDRESS1": "Flat 101",
  "ADDRESS2": "Viman Nagar 1",
  "POSTALCODE": "201018",
  "CITY": "PUNE"
}

Set the variable as NewAddress with value “Updated Address”

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 -> "110034"
}

Result:

{
  "CUSTOMER_ID": 1,
  "CUSTOMER_NAME": "TEST11",
  "ADDRESS1": "Updated Adress",
  "ADDRESS2": "Updated Adress",
  "POSTALCODE": "110034",
  "CITY": "PUNE"
}

 

Conditional Update

Using Update operator you can update the value for a specific field on the basis of some condition

we can see below the fields are updated

Sample Input:

[
  {
    "CUSTOMER_ID": 1,
    "CUSTOMER_NAME": "TEST11",
    "ADDRESS1": "Flat 101",
    "ADDRESS2": "Viman Nagar 1",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST12",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 2",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  }
]

Dataweave Expression:

%dw 2.0
output application/json
---
payload map ((user) ->
  user update {
  case ADDRESS1 at .ADDRESS1 if(ADDRESS1 contains "101") -> ADDRESS1 ++ " UK"
  case ADDRESS2 at .ADDRESS2 if(ADDRESS2 contains "Viman") -> ADDRESS2 ++ " UK"
}
)

Result:

[
  {
    "CUSTOMER_ID": 1,
    "CUSTOMER_NAME": "TEST11",
    "ADDRESS1": "Flat 101 UK",
    "ADDRESS2": "Viman Nagar 1 UK",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST12",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 2 UK",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  }
]

 

namesOf

This function is used to return array of string for all the keys inside a object, it works on the object and return array

Sample Input:

{
  "CUSTOMER_ID": 1,
  "CUSTOMER_NAME": "TEST11",
  "ADDRESS1": "Updated Adress",
  "ADDRESS2": "Updated Adress",
  "POSTALCODE": "110034",
  "CITY": "PUNE"
}

Dataweave Expression:

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

Result:

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

 

valuesOf

This function is used to return array of string for all the values inside a object, it works on the object and return array

Sample Input:

{
  "CUSTOMER_ID": 1,
  "CUSTOMER_NAME": "TEST11",
  "ADDRESS1": "Flat 101",
  "ADDRESS2": "Viman Nagar 1",
  "POSTALCODE": "201018",
  "CITY": "PUNE"
}

Dataweave Expression:

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

Result:

{
  "allvalues": [
    1,
    "TEST11",
    "Flat 101",
    "Viman Nagar 1",
    "201018",
    "PUNE"
  ]
}

 

withMaxSize

This 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

With below example we can see we have extracted the string value for fields like CUSTOMER_NAME,ADDRESS1,ADDRESS2

Sample Input:

[
  {
    "CUSTOMER_ID": 1,
    "CUSTOMER_NAME": "TEST11",
    "ADDRESS1": "Flat 101",
    "ADDRESS2": "Viman Nagar 1",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST12",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 2",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  }
]

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 4,
  "ADDRESS1":$.ADDRESS1 withMaxSize 5,
  "ADDRESS2":$.ADDRESS2 withMaxSize 5
}

Result:

[
  {
    "CUSTOMER_ID": 1,
    "CUSTOMER_NAME": "TEST",
    "ADDRESS1": "Flat ",
    "ADDRESS2": "Viman"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST",
    "ADDRESS1": "Flat ",
    "ADDRESS2": "Viman"
  }
]

 

Upserting Using Update Operator

Update operator enables you to 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

Sample Input:

[
  {
    "CUSTOMER_ID": 1,
    "CUSTOMER_NAME": "TEST11",
    "ADDRESS1": "Flat 101",
    "ADDRESS2": "Viman Nagar 1",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST12",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 2",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST12",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 2",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  }
]

Dataweave Expression:

%dw 2.0
output application/json
---
payload map ((user) ->
  user update {
  case EMAIL at .EMAIL! -> if(EMAIL == null) "[email protected]" else EMAIL
  })

Result:

[
  {
    "CUSTOMER_ID": 1,
    "CUSTOMER_NAME": "TEST11",
    "ADDRESS1": "Flat 101",
    "ADDRESS2": "Viman Nagar 1",
    "POSTALCODE": "201018",
    "CITY": "PUNE",
    "EMAIL": "[email protected]"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST12",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 2",
    "POSTALCODE": "201018",
    "CITY": "PUNE",
    "EMAIL": "[email protected]"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST12",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 2",
    "POSTALCODE": "201018",
    "CITY": "PUNE",
    "EMAIL": "[email protected]"
  }
]

 


Post Submitted by:

Vinod Verma
Integration Specialist at Capgemini Sweden
LinkedIn Profile
  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Kasim
Kasim
4 years ago

Nice docs dude

Victor Manhani
Victor Manhani
3 years ago

the clearly doc that I have seen *-*

Birhane
Birhane
3 years ago

It is really interesting way of presenting topics, I enjoyed clear and preside doc.

Joaquín Ponte
Joaquín Ponte
2 years ago

Simple and clear