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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Exploring C++23: Multidimensional Subscript Operator
  • Java String: A Complete Guide With Examples
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically

Trending

  • Reading GC Logs: Memory Leak or Undersized Heap?
  • Building an Offline Outbox in React Native That Doesn't Lose Writes
  • What Is Plagiarism? How to Avoid It and Cite Sources
  • The Cron Job Stopped and Nothing Turned Red

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Difference Between Update Function and Update Operator in Dataweave

Difference Between Update Function and Update Operator in Dataweave

The new Update function of Dataweave is beneficial to update an object. Below are some of the useful ways to use the update function.

By 
Radhika A
·
Dec. 07, 20 · Opinion
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The new Update function of Dataweave is beneficial to update an object. Below are some of the useful ways to use the update function.

To use the update function, we need to import the package dw::util::Values. The other functions available in this package are attr, mask, index, field. I am providing an example of how we can use the update function in this article.

The update function can be used to update an element of each object in an array. In the below example, we have an array of employees. Each employee has an employee name, age, ServiceDetails. ServiceDetails is again an array. If we want to update the designation element of ServiceDetails, we can use the below data weave. This will update all the elements of the array.

Input:

Java
 




x
51


 
1
[
2

          
3
{
4

          
5
    "EmpName": "John",
6

          
7
    "Age": "32",
8

          
9
    "ServiceDetails":{
10

          
11
        "Designation": "Clerk",
12

          
13
        "DOJ": "Sept 20 1998"
14

          
15
    }
16

          
17
},
18

          
19
{
20

          
21
    "EmpName": "Chrissy",
22

          
23
    "Age": "32",
24

          
25
    "ServiceDetails":{
26

          
27
        "Designation": "Clerk",
28

          
29
        "DOJ": "Sept 20 2000"
30

          
31
    }
32

          
33
},
34

          
35
{
36

          
37
    "EmpName": "Kelvin",
38

          
39
    "Age": "33",
40

          
41
    "ServiceDetails":{
42

          
43
        "Designation": "Accountant",
44

          
45
        "DOJ": "Sept 20 2015"
46

          
47
    }
48

          
49
}
50

          
51
]



Dataweave Code for Update function:

Java
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
import * from dw::util::Values
3
output application/json
4
---
5
payload.ServiceDetails update  "Designation" with "Teacher"



Output:

Java
 




xxxxxxxxxx
1
28


 
1
[
2
  {
3
    "Designation": "Teacher",
4
    "DOJ": "Sept 20 1998"
5
  },
6
  {
7
    "Designation": "Teacher",
8
    "DOJ": "Sept 20 2000"
9
  }
10
  {
11
    "Designation": "Teacher",
12
    "DOJ": "Sept 20 2015"
13
  }
14
]
11
    "Designation": "Teacher",



As the output shows, the Designation of all elements is updated with Teacher.

Using the Update Operator

Update operator is used to updating specified fields of a data structure with new values.

If the requirement is to update an element on some condition, we use the update operator. Like in the above input, I would like to update the designation with "Assistant Manager" if the designation is "Clerk." Below is the dataweave code:

Java
 




xxxxxxxxxx
1
14
9


 
1
%dw 2.0
2
output application/json
3
---
4
payload map ((item) ->
5
    item.ServiceDetails update {
6
        case Designation at .Designation if (Designation == "Clerk") ->  "Assistant Manager"
7
})



Output:

Java
 




xxxxxxxxxx
1
28


 
1
[
2
  {
3
    "Designation": "Assistant Manager",
4
    "DOJ": "Sept 20 1998"
5
  },
6
  {
7
    "Designation": "Assistant Manager",
8
    "DOJ": "Sept 20 2000"
9
  },
10
  {
11
    "Designation": "Accountant",
12
    "DOJ": "Sept 20 2015"
13
  }
14
]
11
    "Designation": "Accountant",



I would be happy to answer any questions.

Operator (extension) Data structure

Opinions expressed by DZone contributors are their own.

Related

  • Exploring C++23: Multidimensional Subscript Operator
  • Java String: A Complete Guide With Examples
  • Mule 4 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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