0

I have the following JSON and am trying to write a JSON Path expression which will return me the isbn number when I have a id of either '123456789' or '987654321'. I tried the following but this did not work. Can anybody tell me what I am doing wrong please. Thanks in advance

JSON Path Expression

$.books[?(@.ids== '123456789' )].isbnNumber

JSON

{
    "books": [{
        "title": "10",
        "isbnNumber": "621197725636",
        "ids": [
            "123456789",
            "987654321"
        ]
    }]
}
user1107753
  • 1,566
  • 4
  • 24
  • 36

1 Answers1

0

The (more traditional) JSONPath implementations that stick close(r) to Goessner's reference specification do not offer handy functions like in which are available in extended implementations like JayWay's JSONPath.
Using Gatling's JSONPath, one thing we could do if the positions of the Ids in question are fixed is accessing their respective indices directly to make the comparison:

$.books[?(@.ids[0] == "123456789" || @.ids[1] == "987654321")].isbnNumber

This will give you the desired result of your example; however, some books only have one of the two indices, or they Id to compare to shows up on a different position it won't work.

wp78de
  • 18,207
  • 7
  • 43
  • 71
  • Thanks but I am still stuck as to how to do this as the ids are not fixed – user1107753 Oct 25 '20 at 21:30
  • @user1107753 I had the idea to use the array-slice operator `[n,m]` as a workaround, however, this also fails because the `m`-boundary is simply ignored. As it stands, only JSONPath implementations with a `in`-array filter can solve this (e.g. using JayWay's `$.books[?('123456789' in @.ids || '987654321' in @.ids)].isbnNumber`) – wp78de Oct 25 '20 at 23:41