简体   繁体   中英

Dataweave - Replace value of a field in an object

I have a Java Object as input payload:

{
"name"="Michael",
"surname"="Alpha",
"mail"="demo@gmail.com",
"gender"="Male"
}

I want change the gender value keeping the rest of the message :

%dw 2.0
output application/java
---
gender: if(payload.gender == "Male") "" else payload.gender

But it return only the gender field. How can I solve that?

The dataweave script needs to match your output structure and you are only outputting a single gender field.

One quick way yo just modify the current payload is using payload ++ .

If your payload is a map/object it will just replace the key if it exists or adds it if not. Example:

%dw 2.0
output application/java
---
payload ++ {gender: (if (payload.gender == "male") ""  else payload.gender)}

The dataweave script can be simplified since 4.3.0 Runtime version with the followed way:

%dw 2.0
output application/java
---
payload update {
       case .gender -> if(payload.gender == "Male") "" else payload.gender
       
}

Example: example changing field value Mule 4.3

Link doc: change value Mule 4.3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM