JsonPath Not working properly

I am trying to save values to be used for later. I checked if the jsonpath I am using is returning the values I want on https://jsonpath.com/ website. I use the same logic for saving another set of data and that works. I don’t know what’s the problem. Could you please help?

Here is the request I use to save “bookingIds”

def getBookingIds = exec(
    http(getGraphQLName(........json"))
      .post("/graphql")
      .headers(graphqlHeader)
      .body(
        RawFileBody("xxxxxx.json")
      )
      .check(noGraphQLErrors)
      .check(
        jsonPath("$.data.currentUser.bookings.*.id").findAll
          .saveAs("bookingIds")
      )
  )

Here is the json data sample:

{
  "data": {
    "currentUser": {
      "id": "....",
      "bookings": [
        {
          "__typename": "SeatOpenBooking",
          "id": "20377"
        }
        ]
    }
  }
}

Here is the error I am getting:

---- Errors --------------------------------------------------------------------
> jsonPath($.data.currentUser.bookings.*.id).findAll.exists, fou      1 (100.0%)
nd nothing
================================================================================

Hi,
I recomend to use JMESPath (https://jmespath.org) as described in documentation → https://gatling.io/docs/gatling/reference/current/core/check/#jmespath
JSONPath it’s not recomended and can be tricky :slight_smile:

JMESPath solution for:

{
  "data": {
    "currentUser": {
      "id": "....",
      "bookings": [
        {
          "__typename": "SeatOpenBooking",
          "id": "20378"
        },
        {
          "__typename": "SeatOpenBooking",
          "id": "20379"
        },
        {
          "__typename": "SeatOpenBooking",
          "id": "20377"
        }
      ]
    }
  }
}

JMESPath:

data.currentUser.bookings[*].id

you get a array:

[
  "20378",
  "20379",
  "20377"
]
3 Likes

As @GeMi wrote, JSONPath really lacks a proper specification, so interpretation of the original blog post and the expected syntax vary from one implementation to another.

Our interpretation is that the operation for array access is [], not dot, so in your case the correct JSONPath expression is $.data.currentUser.bookings[*].id.

Again as @GeMi wrote, JMESPath is a way better choice when it comes to syntax stability.

Note: purchasing the jsonpath.com domain doesn’t make the implementation used there the reference.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.