New Update Operator in Dataweave 2.3.0 Features and Tutorial
In this article, we will dive into different use case of newly added "update" operator which is only compatible with Mule Runtime 4.3.0 and Studio 7.4.
Join the DZone community and get the full member experience.
Join For FreeMulesoft has recently published a newer version of Mule Runtime 4.3.0 with a lot of new features and existing bug fixes.
Few key features of Mule Runtime 4.3.0 are:
- New threading strategy (UBER)
- New Dataweave version 2.3.0
- New Update operator in DW 2.3.0
- XML streaming
- New JSON List objects streams
- Literal Types (Enums)
- Key performance improvements
- New dataweave reader and writer properties and a lot more.
A full list of updates can be found here.
In this article, we will dive into different use case of newly added "update" operator which is only compatible with Mule Runtime 4.3.0 and Studio 7.4
We will also have a look at how to do the same operation without "update" operator with existing "map" and "mapObject" operators available in Mule Runtime lower than 4.3.0
Summary
The new "update" operator has been added in Mule runtime 4.3.0 for updating part of the Message payload based on a certain condition or matching a key name and keeping the rest payload structure as is.
e.g. If I would just want to change the value for a key "name" only if the value for it is "Harry" and keep the rest objects as is in the Payload then I would be using the new "update" operator in DW 2.3.0
The syntax for update operator in DW 2.3.0 is as below:
<value_to_update> update {
case <variable_name> at <update_expression>[!]? [if(<conditional_expression>)]? -> <new value>
...
}
Here:
value_to_update
represents the original value that is going to be updated.update_expression
is the selector expression that is going to match a given value of thevalue_to_update
variable_name
is the name of the variable that is going to bind to the matchedupdate_expression
existing value.new_value
is the expression with the new value.[if(<conditional_expression>)]?
Ifconditional_expression
is true then the value is updated. This section is optional and only required for an Optional update.[!]
marks the selector as an upsert. When marking the expression with!
if the expression doesn’t match anything, the update operator creates all the required elements and inserts the new value.update_expression
supports multiplecase
expressions.
Few of the examples to explain are below:
Example 1: (Updating an Attribute Value in List of Objects)
Input:
xxxxxxxxxx
[
{
"ID" : 7128506,
"name": "Harry",
"age": 30
},
{
"ID" : 7823940,
"name": "Peter",
"age": 70
},
{
"ID": 7086582,
"name": "Romeo",
"age": 10
}
]
With the update operator:
x
%dw 2.0
output application/json
---
payload map ((user) ->
user update {
case name at .name if(name == "Harry") -> "hello " ++ name
}
)
Without the update operator:
x
%dw 2.0
output application/json
---
payload mapObject((value,key) ->
if(key as String == "name")
{
(key): ("Hello " ++ value) if(value == "Harry") else (value)
}
else {
(key): value
}
)
Output:
xxxxxxxxxx
[
{
"ID" : 7128506,
"name": "Hello Harry",
"age": 30
},
{
"ID" : 7823940,
"name": "Peter",
"age": 70
},
{
"ID": 7086582,
"name": "Romeo",
"age": 10
}
]
Example 2: (Using Update as Upsert)
Input:
x
[{
lastName: "Smith"
},{
lastName: "Gary",
name: "Harry"
}]
With update Operator:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload map((user) ->
user update {
case variable at .name! -> if(variable == null) "JOHN" else upper(variable)
}
)
Without update Operator:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload map((user) ->
user mapObject((value,key) ->
(key): (value)
) ++ if((user filterObject((value,key) ->
(key as String == "name")
)) != {})
({
})else{
name: "JOHN"
})
Output:
x
[
{
"lastName": "Smith",
"name": "JOHN"
},
{
"lastName": "Gary",
"name": "Harry"
}
]
Other examples for update operate can be:
- updating a nested JSON element in an object.
- updating the XML attribute.
- updating a particular index object in the list of objects.
Examples for all of these use cases can be found here.
The new update operator is certainly helpful in cases where an element/object needs to be updated keeping all other as is. The small and easy syntax for this is easy to remember and implement.
Published at DZone with permission of Abhishek Solanki. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments