简体   繁体   English

Mule 4-使用Dataweave 2替换JSON数组中的JSON值

[英]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: 在我的Mule 4流中,我得到一个类似于以下内容的JSON数组:

 [
     {
         "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. 我想将每个JSON对象的message字段的值更改为vars.germanMessage变量。

I've tried to modify it with Dataweave 2 this way: 我试图用Dataweave 2修改它:

%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. 但这会返回仅带有message字段的新JSON消息。

The input type is Array<Object> and output type too. 输入类型是Array<Object> ,输出类型也是。

Is there any way to replace the value without changing the rest of the message? 有什么方法可以替换该值而不更改消息的其余部分?

Yes just use mapObject 是的,只需使用mapObject

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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