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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Effective Java Collection Framework: Best Practices and Tips
  • Microservices With Apache Camel and Quarkus

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Effective Java Collection Framework: Best Practices and Tips
  • Microservices With Apache Camel and Quarkus

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.

Abhishek Solanki user avatar by
Abhishek Solanki
·
Jun. 11, 20 · Tutorial
Like (5)
Save
Tweet
Share
13.17K 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.

Trending

  • How to Submit a Post to DZone
  • DZone's Article Submission Guidelines
  • Effective Java Collection Framework: Best Practices and Tips
  • Microservices With Apache Camel and Quarkus

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: