Dataweave - 过滤字段值是否为空

Dataweave - Filter if a field value is null or not

我读取了一个包含 JSON Array 的文件,如下所示:

[
   {
      "example":{
         "name":"edward",
         "lastName":"espin"
      },
      "error":"That name doesn't exists"
   },
   {
      "example":{
         "name":"toretto",
         "lastName":"brav"
      },
      "error":null
   }
]

我想在新文件中存储只有错误值不为空的记录使用相同的格式。

我使用了贯穿每个 JSON 和内部的 for-each,检查 JSON 是否没有 "error" 字段的空值(payload.error != null).

问题是我使用 for-eachchoice.

预期的输出必须是:

[
   {
      "example":{
         "name":"edward",
         "lastName":"espin"
      },
      "error":"That name doesn't exists"
   }
]

是否有使用转换组件Dataweave 2的更简单的方法?

这非常简单,只需在消息转换消息处理器中使用过滤器运算符即可。

%dw 2.0
output application/json
---
payload filter ((item, index) -> item.error != null)

下面是另一种方式

payload[?($.error != null)]