0

In JsonPath, i am trying to pick attributes from one of the available json nodes. However, i couldn't come up with an expression. I tried using anyOf, but wasn't successful. For instance, i need to pull books from any one of the stores (A or B). I am not interested from which store the books get picked up. Please help.

    "storeA": {
        "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
            }
        ]
    },
    "storeB": {
        "book": [
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "action",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ]
    }
}```
  • 2
    By “pull books”, do you mean “find the books from the first available store?” – Abhijit Sarkar Sep 28 '20 at 17:24
  • Can you please explain the output you want with some examples – Ratish Bansal Sep 28 '20 at 19:00
  • @AbhijitSarkar that's correct. Find the books from the first available store. i was hoping to use anyof, but it didn't work out. Ran into compilation error with this "$.[?.(@ anyof ['storeA','storeB'])].book". – mahen00011 Sep 28 '20 at 21:55
  • @RatishBansal for the above example. i need to display books with titles "Sayings of the Century" and "Sword of Honour", only If storeA is not available in response and only storeB is available, then i need to display titles "Moby Dick" and "The Lord of the Rings" The idea is pick one of the nodes available, and extract the child element details from the very first node that's found. I assumed to do something like this $.[?.(@ anyof ['storeA','storeB'])].book.title, but the expression failed with compilation error. – mahen00011 Sep 28 '20 at 22:06
  • What do you mean by "store not available"? In your sample json, for example, are both stores "available"? – Jack Fleeting Sep 28 '20 at 22:12
  • @JackFleeting, in my example both store nodes are available as storeA, storeB. I need books only from one of the stores whichever appears first. It would be have been easier if store is an array and i have a property "type" : "A" or "type": "B". Unfortunately the service am working to parse has a response structure like this ```class storeDetails { StoreInfo storeA; StoreInfo storeB; } where StoreInfo has List books; ``` – mahen00011 Sep 29 '20 at 00:46
  • I can do the following and it works if one of the store exists in response. `$.['storeA','storeD'].book[?(@.category=='fiction')].author` . However, if both stores exist, i only want to fetch the books from first store in the list provided in query `$.['storeB','storeA'].book[?(@.category=='fiction')].author `. It is okay to return empty value if storeB doesn't have any reference books, even though storeA has reference books (as storeB precedes storeA in lookup and storeB node exists) `$.['storeB','storeA'].book[?(@.category=='reference')].author` – mahen00011 Sep 29 '20 at 12:12

1 Answers1

0

I think you're looking for the wildcard selector. I'm not sure how your Java implementation would do it specifically, but the path for it is $.*.book...

The wildcard property selector .* does the trick. It'll pick up all properties of the object.

gregsdennis
  • 7,218
  • 3
  • 38
  • 71