首页 > 解决方案 > Dataweave - 过滤字段值是否为空

问题描述

我读了一个包含JSON Array这样的文件:

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

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

我使用了一个for-each贯穿每个 JSON 并在内部运行的方法,检查该 JSON 是否没有具有空值 ( payload.error != null) 的“错误”字段。

问题是我for-each在 a 中使用和choice

预期的输出必须是:

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

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

标签: jsonfiltermuledataweave

解决方案


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

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

推荐阅读