# Get last element of JSON tab of objects response

**URL:** https://community.gatling.io/t/get-last-element-of-json-tab-of-objects-response/8074
**Category:** Gatling (Open-Source)
**Created:** [June 28, 2023, 8:44am UTC](https://community.gatling.io/t/get-last-element-of-json-tab-of-objects-response/8074 "2023-06-28T08:44:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ELgmbr](https://avatars.discourse-cdn.com/v4/letter/e/7c8e57/32.png) [@ELgmbr](https://community.gatling.io/u/ELgmbr)
#### Post date: [June 28, 2023, 8:44am UTC](https://community.gatling.io/t/get-last-element-of-json-tab-of-objects-response/8074/1 "2023-06-28T08:44:13Z")

</div>

I have a project where I need to get the last object of a JSON list that is returned in JSON but I can’t get any idea of how to from a .check  
My builder:

```auto
    ChainBuilder getAllOrders = exec(http("Orders: /api/orders")
            .get("/api/orders")
            .header("Authorization", "Bearer ${BearerToken}")
            .check(status().is(200))
            .check(jsonPath("$..id").saveAs("OrderIds")));

```

The response of the get is

```auto
"[
  {
    "id": 3,
    "order_number": "{ord_nb}",
    "date": "{date}",
    "amount": "{amt}"
  },
  {
    "id": 4,
    "order_number": "{ord_nb}",
    "date": "{date}",
    "amount": "{amt}"
  }
]"

```

But actually if I use that check I get the id = 3 but I need to get id = 4  
I tried to check if there was a solution something in the documentation but I didn’t found any method, path that could’ve helped (like: “$.[-1:].id”) or even using length (and I checked the similar blogs but didn’t found any answer)

If any information is missing I could provide what’s needed.  
Thanks in advance

---

<div class="post-metadata">

### Author: ![GeMi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/gemi/32/939_2.png) [@GeMi](https://community.gatling.io/u/GeMi)
#### Post date: [June 28, 2023, 10:12am UTC](https://community.gatling.io/t/get-last-element-of-json-tab-of-objects-response/8074/2 "2023-06-28T10:12:08Z")

</div>

Use [jmesPath](https://gatling.io/docs/gatling/reference/current/core/check/#jmespath) instead jsonPath.  
For you case should help:

```auto
jmesPath("[-1].id")

```

or if you want max value - sometimes values in array can be mixed so you want max value:

```auto
jmesPath("max([].id)")

```

---

<div class="post-metadata">

### Author: ![ELgmbr](https://avatars.discourse-cdn.com/v4/letter/e/7c8e57/32.png) [@ELgmbr](https://community.gatling.io/u/ELgmbr)
#### Post date: [June 28, 2023, 12:43pm UTC](https://community.gatling.io/t/get-last-element-of-json-tab-of-objects-response/8074/3 "2023-06-28T12:43:15Z")

</div>

After some tests it seems like this “jsonPath(”$[-1].id")“/“jsonPath(”$[-1:].id”)" works too (without the first dot)  
but your jmesPath works too so I’ll put your answer as solution.  
Thanks a lot!

---

<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 28, 2023, 12:54pm UTC](https://community.gatling.io/t/get-last-element-of-json-tab-of-objects-response/8074/4 "2023-06-28T12:54:17Z")

</div>

Generally speaking, JsonPath is mess because of the lack of a proper spec. As a result, results and syntax can be different in different implementation (eg online editor in JS vs Gatling).  
I recommend you use JMesPath whenever possible.

---

<div class="post-metadata">

### Author: ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)
#### Post date: [July 28, 2023, 12:54pm UTC](https://community.gatling.io/t/get-last-element-of-json-tab-of-objects-response/8074/5 "2023-07-28T12:54:20Z")

</div>

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