Mule中的Dataweave-更改对象的值数组

Mule中的Dataweave-更改对象的值数组,mule,dataweave,json-value,Mule,Dataweave,Json Value,我在消息转换组件中获得一个有效负载作为输入。它是一个带有OBJCT的数组: [ { "enterprise": "Samsung", "description": "This is the Samsung enterprise", }, { "enterprise": "Apple", "description": "This is the Apple enterprise ",

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

 [
      {
          "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",
      }
  ]

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

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

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

%dw 2.0
output application/java
---
payload map ((item, index) -> 
    item - "description" ++ {description: vars.descriptionValue}
)
否则,您可以使用
mapObject
在每个对象的键值对上迭代,并使用
模式匹配
添加一个
大小写
,以确定键的描述时间。 当我想做很多替换时,我更喜欢第二种方法

%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)
)