简体   繁体   中英

Dataweave - Filter if a field value is null or not

I read a file that contains a JSON Array like this:

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

I want to store in a new file only the records which the error value is not null using the same format.

I have used a for-each that runs through each JSON and inside, checks if that JSON does not have the "error" field with null value ( payload.error != null ).

The problem is that I use a for-each and in a choice .

The output expected must be:

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

Is there a simpler way with a transformation component and Dataweave 2 ?

This is quite simple just use the filter operator in a message transform message processor.

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

下面是另一种方式

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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