Skip to content
Advertisement

response.jsonPath() has square brackets around the element, how do I retrieve the string value? Rest Assured

I have an HTTP response body that looks that this when I make a GET request:

 [
  {
    "id": "1111",
    "type": "Sale",
    "name": "MyNameTest",
    "shortDescription": "Sale a"
  }
]

When I try to assert the results with “Rest Assured”, the name value is always wrapped in square brackets [].

    final String returnedAttributeValue = response.jsonPath().getString("name");
    Assert.assertEquals(returnedAttributeValue, "MyNameTest");

So the test fails with Expected “MyNameTest”, but was “[MyNameTest]”

Can any one tell me how to resolve this?

Advertisement

Answer

You are accessing values within an array, so use name[n]

final String returnedAttributeValue = response.jsonPath().getString("name[0]");
Assert.assertEquals(returnedAttributeValue, "MyNameTest");
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement