Interview #139: RestAssured: How to assert values from a JSON response body?
When working with RestAssured, a popular Java library for testing REST APIs, one of the most fundamental tasks is asserting values from the JSON response body. These assertions help confirm that the API is returning the expected data, structure, and types—ensuring correctness and stability of the backend service.
Disclaimer: For QA-Testing Jobs, WhatsApp us @ 91-9606623245
Let’s break down how to assert JSON response values in RestAssured with examples, strategies, and best practices.
🔹 Basic Workflow
✅ 1. Asserting Simple JSON Fields
For a basic JSON response like this:
{
"id": 101,
"name": "John Doe",
"status": "active"
}
You can write simple assertions using body() and Hamcrest matchers:
given()
.when()
.get("/users/101")
.then()
.statusCode(200)
.body("id", equalTo(101))
.body("name", equalTo("John Doe"))
.body("status", equalTo("active"));
Here, RestAssured uses JsonPath internally to navigate and extract values from the JSON.
✅ 2. Asserting Nested JSON Fields
For a nested structure like:
{
"user": {
"id": 101,
"profile": {
"name": "Jane",
"email": "jane@example.com"
}
}
}
Use dot notation to assert nested fields:
given()
.when()
.get("/user/profile")
.then()
.body("user.id", equalTo(101))
.body("user.profile.name", equalTo("Jane"))
.body("user.profile.email", equalTo("jane@example.com"));
Dot notation makes it easy to traverse deeply nested elements.
✅ 3. Asserting Elements in JSON Arrays
For a JSON array response like:
{
"products": [
{ "id": 1, "name": "Laptop" },
{ "id": 2, "name": "Phone" }
]
}
You can assert values inside arrays using index-based access:
given()
.when()
.get("/products")
.then()
.body("products[0].name", equalTo("Laptop"))
.body("products[1].id", equalTo(2));
You can also check list size:
.body("products.size()", equalTo(2));
Or assert that a list contains certain values:
Recommended by LinkedIn
.body("products.name", hasItems("Laptop", "Phone"));
✅ 4. Using JsonPath for Dynamic Extraction and Assertions
Sometimes, especially in complex responses, you’ll want to extract values programmatically before making assertions.
Response response = given()
.when()
.get("/user/101");
JsonPath jsonPath = response.jsonPath();
String email = jsonPath.getString("user.profile.email");
assertEquals("jane@example.com", email);
This is useful when:
✅ 5. Deserializing JSON into POJOs
Another approach is to convert the JSON into a Java object and assert fields using regular Java assertions.
public class User {
public int id;
public String name;
public String status;
}
User user = given()
.when()
.get("/users/101")
.then()
.extract()
.as(User.class);
assertEquals(101, user.id);
assertEquals("John Doe", user.name);
This is a clean and maintainable approach for large or reusable structures.
✅ 6. Using Custom Matchers or Assertions
You can use Hamcrest or AssertJ to create more complex assertions:
.body("users.findAll { it.age > 18 }.size()", greaterThan(3))
Or use TestNG/JUnit:
String name = response.jsonPath().getString("name");
Assert.assertTrue(name.startsWith("J"));
🔹 Best Practices
🔹 Common Assertion Matchers in RestAssured
🔹 Conclusion
RestAssured provides a powerful, expressive way to assert JSON response bodies using:
Whether you're validating flat fields, deeply nested elements, or arrays, RestAssured makes it clean and readable. By combining these techniques with structured test scenarios, you can build robust and maintainable API test suites.
QA Automation Engineer|Streamlining QA Processes with Robust Automation Solutions
1wVery well explained 👍