Update Operator Examples

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_update represents the original value to update.
  • update_expression is the selector expression that matches a value in value_to_update.
  • variable_name is the name of the variable to bind to the matched update_expression value.
  • new_value is the expression with the new value.
  • [if(<conditional_expression>)]? If conditional_expression returns true, 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 the update operator create all the required elements and insert the new value.
  • update_expression supports multiple case expressions.

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.

Instagram

Facebook

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago