13

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?

Kristijan Iliev
  • 4,747
  • 9
  • 24
  • 45
Mike Chinaloy
  • 1,336
  • 2
  • 18
  • 40

3 Answers3

24

The Jayway implementation supports inlined AND and OR criteria.

$..book[?(@.price==8.99 && @.category=='fiction')]  

[
   {
      "category" : "fiction",
      "author" : "Herman Melville",
      "title" : "Moby Dick",
      "isbn" : "0-553-21311-3",
      "price" : 8.99
   }
]

Try it out and compare different implementations here http://jsonpath.herokuapp.com/

kalle
  • 1,611
  • 11
  • 14
  • Awesome thank you very much. The jspnpath online implementation is very clean and works great. – Beezer Nov 27 '17 at 19:58
7

below is an example showing JSON path with AND and OR criteria:

Get all books with price less than 10 and of 'fiction' category

$..book[?(@.price<10 && @.category=='fiction')]

Get all books with price less than 10 or any book of 'fiction' category

$..book[?(@.price<10 || @.category=='fiction')]  

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

$..book[?(@.author=='Herman Melville' && (@.price<10 || @.category=='fiction'))]
4

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:

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

And the expression:

$..book[?(@.price==8.99),?(@.category=='fiction')]

which results in:

[  
   {  
      "category":"fiction",
      "author":"Herman Melville",
      "title":"Moby Dick",
      "isbn":"0-553-21311-3",
      "price":8.99
   }
]

what seems to be perfect. (take the price 8.99 AND category 'fiction'... perfect!)

BUT: change it to:

$..book[?(@.category=='fiction'),?(@.price==8.99)]

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!

stefankmitph
  • 3,038
  • 2
  • 16
  • 22
  • Thanks, you've pretty much confirmed my suspicions and its good to know that I wasn't doing something completely silly. For my use case it seems that it just doesn't work at all even if I try switching the attributes around. – Mike Chinaloy Apr 17 '15 at 16:21
  • Alternatively I found that I could achieve what I needed to by using Filter Predicates in my Java code – Mike Chinaloy Apr 17 '15 at 16:29
  • yeah... if you got something (like java, c#...) to further process the result(s) it's not too bad that the operators are not working... – stefankmitph Apr 17 '15 at 16:40
  • 1
    According to http://goessner.net/articles/JsonPath/index.html#e2 the operator [,] performs an union. Which means it will work as an OR instead of the AND that question refers. – Zé Carlos Feb 26 '18 at 11:59