Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dataweave - Replace value of a field in an object

I have a Java Object as input payload:

{
"name"="Michael",
"surname"="Alpha",
"mail"="[email protected]",
"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?

like image 777
gtx911 Avatar asked Sep 14 '25 23:09

gtx911


2 Answers

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)}
like image 156
Ryan Carter Avatar answered Sep 16 '25 12:09

Ryan Carter


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

like image 45
Stefano Avatar answered Sep 16 '25 12:09

Stefano