Search code examples
rest-assuredrest-assured-jsonpath

Rest Assured access last element in list using JsonPath


I want to use RestAssured to test an endpoint that returns a JSON list of objects. How can I verify the contents of the last object in the list using RestAssured JsonPath?

Example return value:

[
 {
    "foo": "bar"
 },
 {
    "foo": "brazz"
 } 
]

I tried using negative indices: body("[-1].foo", equalTo("brazz")) but that does not work, as Rest assured considers "[-1].foo" to be an empty list.

How can I access the last element of a list with JsonPath Syntax?


Solution

  • Yes, you can use last() to get the last item.

    body("last().foo", equalTo("brazz"))