Hleb
Hleb

Reputation: 7371

Issue with filtering of JSON nodes with JsonPath

I have JSON with such structure:

{
    "stores": {
        "store1_id": {
            "books": {
                "book1_ISBN": {
                    "category": "reference",
                    "author": "Nigel Rees",
                    "title": "Sayings of the Century",
                    "price": "8.95"
                },
                "book2_ISBN": {
                    "category": "fiction",
                    "author": "Evelyn Waugh",
                    "title": "Sword of Honour",
                    "price": "12.99"
                }
            }
        },
        "store2_id": {
            "books": {
                "book3_ISBN": {
                    "category": "fiction",
                    "author": "J. R. R. Tolkien",
                    "title": "The Lord of the Rings",
                    "isbn": "0-395-19395-8",
                    "price": "22.99"
                }
            }
        }
    }
}

and I parse it with Jayway JsonPath. I try to get all books with fiction category using such expression:

$[?(@.stores.*.books)].stores.*.books[?(@.*.category=="reference")]

but I get empty result, although according equal operator documentation I expect that I will receive "Sayings of the Century" book node:

"book1_ISBN": {
    "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": "8.95"
}

Interesting, that's when I use this expression without equals operator:

$[?(@.stores.*.books)].stores.*.books[?(@.*.category)]

I get all three books in result. Why my expression with equal operator don't work?

Upvotes: 0

Views: 190

Answers (1)

marco
marco

Reputation: 681

I tried your expressions here: http://jsonpath.com/? and they don't work, both of them, with filter and without filter.
I do not understand why you first filter these elements: (@.stores.*.books)
Anyway if you just want to filter all the books with category "fiction", this is simpler:

$..[?(@.category=="fiction")]

I hope this help.

Upvotes: 1

Related Questions