Skip to content
CodeKruPro

Validate nested JSON fields

Automation taskEasy-MediumJava onlyAsked in: Capgemini, CloudSEK, Swiggy, Wipro

Interviewers paste a nested response and ask you to assert the city of the user's address. This lesson practises GPath paths such as address.city, the path language REST Assured uses to read JSON.

Validate Nested JSON

Validate Nested JSON

JSON responses often have nested objects. Use dot notation to access nested fields:

.body("address.city", equalTo("Springfield"))  // object inside an object
.body("address.geo.verified", equalTo(true))  // one level deeper

The /products/1 endpoint returns:

{"id": 1, "name": "Laptop Pro", "price": 1299.99,
 "category": {"id": 1, "name": "electronics"},
 "specs": {"weight": "1.5kg", "color": "silver", "inStock": true}}

Task: Validate the product name, category name, specs color, and inStock status.

Expected output:

Product: Laptop Pro
Category: electronics
In Stock: true