# How to extract the last array element in jsonpath with findAll

**URL:** https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867
**Category:** Gatling (Open-Source)
**Tags:** gatling-academy
**Created:** [June 23, 2022, 10:24pm UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867 "2022-06-23T22:24:44Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![alexM](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexM](https://community.gatling.io/u/alexM)
#### Post date: [June 23, 2022, 10:24pm UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/1 "2022-06-23T22:24:44Z")

</div>

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

```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

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [June 24, 2022, 7:23am UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/2 "2022-06-24T07:23:07Z")

</div>

You’ll have to use transform to pick the last element of the list: [Gatling - Checks](https://gatling.io/docs/gatling/reference/current/core/check/#transforming)

---

<div class="post-metadata">

### Author: ![alexM](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexM](https://community.gatling.io/u/alexM)
#### Post date: [June 24, 2022, 7:28am UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/3 "2022-06-24T07:28:12Z")

</div>

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)} ?

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [June 24, 2022, 7:42am UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/4 "2022-06-24T07:42:54Z")

</div>

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

---

<div class="post-metadata">

### Author: ![sbrevet](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/sbrevet/32/830_2.png) [@sbrevet](https://community.gatling.io/u/sbrevet)
#### Post date: [June 24, 2022, 8:27am UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/5 "2022-06-24T08:27:30Z")

</div>

Why getting the last after?

jsonpath should let you to get directly the last element:

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

Cheers!

---

<div class="post-metadata">

### Author: ![alexM](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexM](https://community.gatling.io/u/alexM)
#### Post date: [June 24, 2022, 8:57am UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/6 "2022-06-24T08:57:31Z")

</div>

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"
        }
      ]
    }
    ]
  }
}

```

}

---

<div class="post-metadata">

### Author: ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)
#### Post date: [June 24, 2022, 1:59pm UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/7 "2022-06-24T13:59:23Z")

</div>

FYI [Core: Gatling EL access by negative/backward index · Issue #4262 · gatling/gatling · GitHub](https://github.com/gatling/gatling/issues/4262)

Negative index access will be supported in upcoming 3.8.

---

<div class="post-metadata">

### Author: ![alexM](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@alexM](https://community.gatling.io/u/alexM)
#### Post date: [June 24, 2022, 7:15pm UTC](https://community.gatling.io/t/how-to-extract-the-last-array-element-in-jsonpath-with-findall/6867/8 "2022-06-24T19:15:59Z")

</div>

Great @slandelle … We are waiting!

–A
