简体   繁体   中英

Mule 4 - Replace a JSON value in JSON Array with Dataweave 2

In my Mule 4 flow I get a JSON Array similar to the following:

 [
     {
         "type": "error",
         "status": 404,
         "code": "not_found",
         "message": "Could not find the resource",
     },
     {
         "type": "error",
         "status": 401,
         "code": "",
         "message": "Could not find the specified ",
     }
 ]

I want to change the value of the message field to a vars.germanMessage variable for each JSON object.

I've tried to modify it with Dataweave 2 this way:

%dw 2.0
output application/java
 ---
(payload as Array) map {

    "message": vars.germanMessage

}

But this returns a new JSON message with only the message field.

The input type is Array<Object> and output type too.

Is there any way to replace the value without changing the rest of the message?

Yes just use mapObject

payload mapObject (value,key) -> {
    (key): if((key as String) == "message")) vars.germanMessage else value
}

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