1
votes

Here is the JSON Response:

[{
    "startTime": "2020-07-21T15:20:00.000+00:00",
    "endTime": "2020-07-21T15:40:00.000+00:00",
    "availabilities": [{
        "availabilityId": "eyJJZCI6MTA4N",
        "startTime": "2020-07-21T15:20:00.000+00:00",
        "endTime": "2020-07-21T15:40:00.000+00:00",
        "channel": "PHONE",
        "programId": "Msff",
        "providerDetails": {
            "firstName": "abc",
            "lastName": "abc",
            "providerTitle": "NURSE"
        }
    }]
}, {
    "startTime": "2020-07-21T15:40:00.000+00:00",
    "endTime": "2020-07-21T16:00:00.000+00:00",
    "availabilities": [{
        "availabilityId": "eyJJZCI6MTA4NDM2MiwiU3RhcnRUa",
        "startTime": "2020-07-21T15:40:00.000+00:00",
        "endTime": "2020-07-21T16:00:00.000+00:00",
        "channel": "PHONE",
        "programId": "Msff",
        "providerDetails": {
            "firstName": "def",
            "lastName": "def",
            "providerTitle": "NURSE"
        }
    }]
}]

And here is the check i am using to extract the first "availabilityId" from json response

check(
  jsonPath("$[0][availabilities].[0].availabilityId") saveAs "availabilityId"
)

but i am getting error:

jsonPath($[0][availabilities].[0].availabilityId).find.exists extraction crashed: end of input expected

I validated the path on https://jsonpath.com/, i am able to see the result. What am i doing wrong?

2

2 Answers

2
votes

That's an example of how bad JsonPath is in its current state:

  • JsonPath currently doesn't have a real specification
  • because of holes in the original "paper" (a simple blog post actually) and implementors going with their own interpretation and likings, there are A LOT of differences between implementations
  • jsonpath.com isn't currently a reference, just someone who bought the domain name

Here, if you check the original source, you'll see that the square notation is supposed to use single quotes to wrap field name:

JSONPath expressions can use the dot–notation

$.store.book[0].title

or the bracket–notation

$['store']['book'][0]['title']

What happens here is that the Gatling implementation sticks to this definition while the JavaScript one used on jsonpath.com allows for ditching the single quotes.

Also, you shouldn't have dots between your brackets, so your path should be:

$[0]['availabilities'][0].availabilityId

You could also stick to the more common dot notation:

$[0].availabilities[0].availabilityId

There's an ongoing effort on creating a proper JsonPath implementation. Until this effort lands, we from Gatling recommend going with JMESPath instead, as explained here. On contrary to JsonPath atm, JMESPath has a real complete grammar and a compliance test suite.

0
votes

You have added unnecessary square brackets. Change jsonPath on:

$.[0].availabilities.[0].availabilityId