繁体   English   中英

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

[英]Dataweave in Mule - Change value Array of Objects

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

 [
      {
          "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遍历每个对象的键值对,并通过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)
)

暂无
暂无

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

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