we子中的Dataweave-更改对象的值数组

时间:2019-04-09 15:21:35

标签: mule dataweave json-value

我在消息转换组件中获得了有效负载作为输入。它是带有对象的数组:

 [
      {
          "enterprise": "Samsung",
          "description": "This is the Samsung enterprise",
      },
      {
          "enterprise": "Apple",
          "description": "This is the Apple enterprise ",
      }
  ]

我有一个变量来代替描述,而我想要的输出是:

[
      {
          "enterprise": "Samsung",
          "description": "This is the var value",
      },
      {
          "enterprise": "Apple",
          "description": "This is the var value",
      }
  ]

我尝试使用:

 %dw 2.0
 output application/java
 ---
 payload map ((item, index) -> {

     description: vars.descriptionValue
 })

但是它返回:

 [
      {
          "description": "This is the var value",
      },
      {
          "description": "This is the var value",
      }
  ]

是否可以仅替换描述值,并保留其余字段? 避免在映射中添加其他字段

1 个答案:

答案 0 :(得分:2)

有很多方法可以做到这一点。

一种方法是首先删除原始描述字段,然后添加新的描述字段。

%dw 2.0
output application/java
---
payload map ((item, index) -> 
    item - "description" ++ {description: vars.descriptionValue}
)

否则,您可以使用mapObject遍历每个对象的键值对,并用pattern matching添加一个case来描述键。 当我想进行很多替换时,我更喜欢使用第二种方法。

%dw 2.0
output application/java
fun process(obj: Object) = obj mapObject ((value, key) -> {
    (key): key match {
        case "description" -> vars.descriptionValue
        else -> value
    }
})
---
payload map ((item, index) -> 
    process(item)
)
相关问题