Search code examples
scalagatlingjsonpath

Scala - Gatling - JsonPath - Check if object with specific value exist


I have a jsonpath respone like:

{
  "pageable": {
    "currentPage": 1,
    "totalPages": 1,
    "pageSize": 20,
    "last": true,
    "first": true
  },
  "sort": {
    "orders": [],
    "sorted": false,
    "unsorted": true,
    "empty": true
  },
  "totalElements": 6,
  "data": [
    {
      "id": 1,
      "roleName": "test1",
      "userCount": 5
    },
    {
      "id": 2,
      "roleName": "test2",
      "userCount": 5
    }
  ]
}

I want to check that the data with id == 1 exist. I tried some combinations like:

checks ::= jsonPath("$..data.*[?(@.id=='1')]").exists
checks ::= jsonPath("$.data.*[?(@.id=='1')]").exists
checks ::= jsonPath("$..data[?(@.id=='1')]..id").exists
checks ::= jsonPath("$..data[?(@.id=='1')].id").exists
checks ::= jsonPath("$.data[?(@.id=='1')].id").exists

But no path works, I'm getting error:

jsonPath($..data..*[?(@.id=='1')]).find.exists, found nothing 

Any idea?


Solution

  • Instead of trying to use JsonPath whose syntax is unclear and lacks a standard, you should try JMESPath instead, that Gatling supports as well.

    jmesPath("data[?id == `1`]")