Introduced in DataWeave 2.3.0. Supported by Mule 4.3 and later.
Example
Versions prior to Mule 4.3 require the following syntax to increase the age value in myInput by one without recreating the entire object:
Source
%dw 2.0
var myInput = {
"name": "Ken",
"lastName":"Shokida",
"age": 30
}
output application/json
---
myInput mapObject ((value,key) ->
if(key as String == "age")
{(key): value as Number + 1}
else
{(key): value} )
In Mule 4.3, the update operator simplifies the syntax:
Source
%dw 2.0
var myInput = {
"name": "Ken",
"lastName":"Shokida",
"age": 30
}
output application/json
---
myInput update {
case age at .age -> age + 1
}
Both DataWeave scripts return the same result:
Output
{
"name": "Ken",
"lastName": "Shokida",
"age": 31
}
With update, casting or iterating through all the key-value pairs is not necessary.
The update syntax is as follows:
<value_to_update> update {
case <variable_name> at <update_expression>[!]? [if(<conditional_expression>)]? -> <new value>
...
}
Source
%dw 2.0
output application/json
---
payload map ((item) ->
item.ServiceDetails update {
case Designation at .Designation if (Designation == "Clerk") -> "Assistant Manager"
})
Output
[
{
"Designation": "Assistant Manager",
"DOJ": "Sept 20 1998"
},
{
"Designation": "Assistant Manager",
"DOJ": "Sept 20 2000"
},
{
"Designation": "Accountant",
"DOJ": "Sept 20 2015"
}
]
value_to_updaterepresents the original value to update.update_expressionis the selector expression that matches a value invalue_to_update.variable_nameis the name of the variable to bind to the matchedupdate_expressionvalue.new_valueis the expression with the new value.[if(<conditional_expression>)]?Ifconditional_expressionreturnstrue, the script updates the value. Use of a conditional expression is optional.[!]marks the selector as an upsert. If the expression doesn’t find a match, the!makes theupdateoperator create all the required elements and insert the new value.update_expressionsupports multiplecaseexpressions.
| Note The update operator doesn’t mutate the existing value. Instead, the operator creates a new value with the updated expressions. |
Follow Me
If you like my post please follow me to read my latest post on programming and technology.

Leave a Comment
You must be logged in to post a comment.