{{announcement.body}}
{{announcement.title}}

New Update Operator in Dataweave 2.3.0 Features and Tutorial

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

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.

Topics:
dataweave, dataweave 2, integration, mule 4, tutorial, update

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}