Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression to filter out elements with empty arrays using `jsonPath`

I have a JSON payload of the form:

[
  {"id": 1, "list": [1], "name":"one"}, 
  {"id": 2, "list": [1,2], "name":"two"},
  {"id": 3, "list": [], "name":"three"}
]

And I want to filter out the element from the array the contains an empty "list" property. In other words, I want to discard the element with id=3 and process only the first and second element in my example above.

Currently, my filter looks like this:

<!-- ne == not equals -->
<int:filter id="filter" 
            input-channel="in" 
            output-channel="out" 
            expression="#jsonPath(payload, '$[*].list') ne '[]'"
            discard-channel="consoleOutputChannel" />

But this is not working, how should I indicate to my expression that I want to exclude elements with empty list properties?

like image 341
blurfus Avatar asked Oct 13 '25 02:10

blurfus


1 Answers

Change your expression to:

$.[?(@.list.length()> 0)]
  • [?(<expression>)] : filter expression
  • @ : The current node being processed by a filter predicate
  • list.length() : the length of list array

More detail at JsonPath

like image 117
Viet Avatar answered Oct 14 '25 14:10

Viet