gtx911
gtx911

Reputation: 1289

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?

Upvotes: 2

Views: 4319

Answers (2)

Bharath Mandava
Bharath Mandava

Reputation: 11

Below is another way

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

Upvotes: 1

machaval
machaval

Reputation: 5059

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)

Upvotes: 5

Related Questions