I have an array in JSON and I would like to apply two filters to it like so:
$._embedded.values[0]._embedded.data[?(@.var1='value1' && @.var2='value2')]
I need to select only those elements from the array that satisfies the AND operation. However, in practice this doesn't appear to work.
Is it possible to do this or must I perform a two step action to extract one set then filter again to get my final results?
As far as I know AND / OR are not supported yet. But you can get relatively close with this [,] array. But even this made me wonder when I tested it. There's some strange behaviour. I took an example JSON string from the Jayway JsonPath Evaluator and put it into JsonPath Expression Tester (because it didn't work with Jayway when I tried).
So the JSON I've tested with the curiousconcept version is:
And the expression:
which results in:
what seems to be perfect. (take the price 8.99 AND category 'fiction'... perfect!)
BUT: change it to:
then the output is:
It seems to me that [,] is not well implemented in Jayway JsonPath Tester as well as curiousconcept.com expression tester.
But from what I know they're working on an (real) implementation of AND and or (issue27 here at google code).
Hope this clarifies at least something!
below is an example showing JSON path with AND and OR criteria:
Get all books with price less than 10 and of 'fiction' category
Get all books with price less than 10 or any book of 'fiction' category
You can also try mixing up AND and Or criteria:
Get books of 'Herman Melville' with with price less than 10 or his book of 'fiction' category
The Jayway implementation supports inlined AND and OR criteria.
Try it out and compare different implementations here http://jsonpath.herokuapp.com/