4

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?

2
  • use .length() ? Commented May 10, 2017 at 4:23
  • How would the syntax go? I think I already tried it and didn't work Commented May 10, 2017 at 4:30

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

9 Comments

I will try when I get back at my desk (tomorrow) and report back.. .thx
please look at How to Answer
I don't understand why it is not provide an answer? But nvm, i will update more details..
do you think it's a good a idea to post a link which may become invalid?
what means invalid?
|
0

With the help of @Jerry06, I was able to do what I wanted but only after splitting the payload array.

This is the complete solution but I could only do it because of his help (thx) so the credits are to him.

My code now looks like this:

<!-- Split payload into individual messages -->
<int:splitter id="splitter"
                  input-channel="in"
                  output-channel="toFilter"
                  expression="#jsonPath(payload, '$.[*]')"/>

<!-- Filters out messages with empty array of list -->
<int:filter id="filter"
            input-channel="toFilter"
            output-channel="out"
            discard-channel="consoleOutputChannel"
            expression="#jsonPath(payload, '$.list.length()') > 0"/> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.