How to extract the last array element in jsonpath with findAll

Hi,
I have an array of values and from that Array(which might have more than one value) I always want to choose the last value. How to do it
Example:
In the below JSON I want to select the Last array of id value
check(jsonpath(“$.data.A.main[].attempts[].id”).findAll.saveAs(“id”))
As this array is not static so I want to choose the last value of the array always.

sample JSON

{
  "data": {
    "A": {
      "A1": {
        "colour": "1",
        
      },
      "main": [
        {
          "Type": "1",
          "attempts": [
            {
              "id": "Abc1"
            }
          ]
        },
        {
          "Type": "2",
          "attempts": [
            {
              "id": "def27"
            }
          ]
        },
        {
          "Type": "3",
          "attempts": [
            {
              "id": "rtyw12"
            }
          ]
        },
        {
          "Type": "4",
          "attempts": [
            {
              "id": "98dg"
            }
          ]
        }
        ]
      }
    }
  }

–A

You’ll have to use transform to pick the last element of the list: Gatling - Checks

Or can I save the array in attribute id – check(jsonpath(“$.data.A.main[].attempts[ ].id”).findAll.saveAs(“id”))

if I use -1 will the attribute array use the last value #{id(-1)} ?

No, Gatling EL doesn’t support negative index access. Contributions welcome :slight_smile:

Why getting the last after?

jsonpath should let you to get directly the last element:

$.data.A.main[-1].attempts[-1].id

Cheers!

Hi @sbrevet
The id data will be updated after progression of each task. But the structure will remain with id as null and also the Type is not always 4, its array will change depending upon the users.

Fetching directly the Last value is the best way. or I need to fetch based on the Type value in the json.

{
“data”: {
“A”: {
“A1”: {
“colour”: “1”,

  },
  "main": [
    {
      "Type": "1",
      "attempts": [
        {
          "id": "Abc1"
        }
      ]
    },
    {
      "Type": "2",
      "attempts": [
        {
          "id": "def27"
        }
      ]
    },
    {
      "Type": "3",
      "attempts": [
        {
          "id": "null"
        }
      ]
    },
    {
      "Type": "4",
      "attempts": [
        {
          "id": "null"
        }
      ]
    }
    ]
  }
}

}

FYI Core: Gatling EL access by negative/backward index · Issue #4262 · gatling/gatling · GitHub

Negative index access will be supported in upcoming 3.8.

Great @slandelle … We are waiting!

–A