DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails
  • Agentic AI: The Next Evolution of Artificial Intelligence and Autonomous Automation
  • Exploring C++23: Multidimensional Subscript Operator

Trending

  • How to Write for DZone Publications: Trend Reports and Refcards
  • The Lethal Trifecta Is Hiding in Your MCP Server
  • Building a Voice-Controlled Graph Assistant With Neo4j, LiveKit, and OpenAI
  • Performance Testing With JMeter Beyond the Basics: Distributed Load, Realistic Profiles, and Identifying Security Bottlenecks

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.

By 
Abhishek Solanki user avatar
Abhishek Solanki
·
Jun. 11, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
16.1K Views

Join the DZone community and get the full member experience.

Join For Free

Mulesoft 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:

  1. New threading strategy (UBER)
  2. New Dataweave version 2.3.0
  3. New Update operator in DW 2.3.0
  4. XML streaming
  5. New JSON List objects streams
  6. Literal Types (Enums)
  7. Key performance improvements
  8. 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:

Java
 




x


 
1
<value_to_update> update {
2
    case <variable_name> at <update_expression>[!]? [if(<conditional_expression>)]? -> <new value>
3
    ...
4
}


 

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 the value_to_update

  • variable_name is the name of the variable that is going to bind to the matched update_expression existing value.

  • new_value is the expression with the new value.

  • [if(<conditional_expression>)]? If conditional_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 multiple case expressions.

Few of the examples to explain are below:

Example 1: (Updating an Attribute Value in List of Objects)

Input: 

JSON
 




xxxxxxxxxx
1
17


 
1
[
2
   {
3
       "ID" : 7128506,
4
       "name": "Harry",
5
       "age": 30
6
   },
7
   {
8
       "ID" : 7823940,
9
       "name": "Peter",
10
       "age": 70
11
   },
12
   {
13
       "ID": 7086582,
14
       "name": "Romeo",
15
       "age": 10
16
   }
17
]



With the update operator:

Java
 




x


 
1
%dw 2.0
2
output application/json
3
---
4
payload map ((user) ->
5
   user update {
6
       case name at .name if(name == "Harry") -> "hello " ++ name
7
   }
8
)



Without the update operator:

Java
 




x


 
1
%dw 2.0
2
output application/json
3
---
4
payload mapObject((value,key) -> 
5
    if(key as String == "name")
6
    {
7
        (key): ("Hello " ++ value) if(value == "Harry") else (value)
8
    }
9
    else {
10
        (key): value
11
    }
12
)



Output:

JSON
 




xxxxxxxxxx
1
17


 
1
[
2
   {
3
       "ID" : 7128506,
4
       "name": "Hello Harry",
5
       "age": 30
6
   },
7
   {
8
       "ID" : 7823940,
9
       "name": "Peter",
10
       "age": 70
11
   },
12
   {
13
       "ID": 7086582,
14
       "name": "Romeo",
15
       "age": 10
16
   }
17
]



Example 2: (Using Update as Upsert)

Input:

JSON
 




x


 
1
[{
2
    lastName: "Smith"
3
},{
4
    lastName: "Gary",
5
    name: "Harry"
6
}]



With update Operator:

Java
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
payload map((user) ->
5
    user update {
6
        case variable at .name! -> if(variable == null) "JOHN" else upper(variable)
7
    }
8
)



Without update Operator:

Java
 




xxxxxxxxxx
1
13


 
1
%dw 2.0
2
output application/json
3
---
4
payload map((user) -> 
5
    user mapObject((value,key) ->
6
        (key): (value)
7
    ) ++ if((user filterObject((value,key) -> 
8
        (key as String == "name")
9
    )) != {})
10
    ({
11
})else{
12
    name: "JOHN"
13
})



Output:

JSON
 




x


 
1
[
2
  {
3
    "lastName": "Smith",
4
    "name": "JOHN"
5
  },
6
  {
7
    "lastName": "Gary",
8
    "name": "Harry"
9
  }
10
]



Other examples for update operate can be:

  1. updating a nested JSON element in an object.
  2. updating the XML attribute.
  3. 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.

Operator (extension)

Published at DZone with permission of Abhishek Solanki. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails
  • Agentic AI: The Next Evolution of Artificial Intelligence and Autonomous Automation
  • Exploring C++23: Multidimensional Subscript Operator

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook