About this lesson
REST Assured nested JSON validation with dot paths in body()
Learn REST AssuredBeginnerResponse Body Validation
How it works
Real API payloads nest objects inside objects. A product here carries a category object and a specs object, and REST Assured reaches into them with dots: category.name means the name key inside category. Any depth works the same way, one segment per level.
When a segment is wrong, nothing throws on the path itself. Whether the parent is misspelt or the child, the value resolves to null, and the assertion message reports "Actual: null" for the full path you gave. If several checks share a parent, rootPath() on the ValidatableResponse sets a prefix for the following body() calls, and noRootPath() clears it.
JSON booleans become Java Boolean objects, so equalTo(true) matches while the quoted text true does not. Decimals become Float, so a price compared with a double literal fails with the actual value printed with an F suffix.
About the assignment
Product 1 is a laptop with nested category and specs objects. You verify four values across those levels in a single chain, then print the product name, the category and the stock flag.
What you will do
- Request
/products/1and move tothen(). - Check the top-level
nameand the nestedcategory.namewithequalTo(). - Check
specs.colorandspecs.inStock, the latter against a boolean, not a string. - Print the three labelled lines from the task, the colour check has no output line.
Common mistakes
Quoting the boolean
Matching
specs.inStockagainst the string true fails, and the message shows the actual value in angle brackets because it is aBoolean. Pass the boolean literal instead.Expecting an error for a bad parent key
A path like
spec.colordoes not complain thatspecis missing, it just yieldsnull. Read the failure message carefully to find which segment is wrong.Asserting the price with a double
equalTo()with 1299.99 fails against the parsedFloat. Compare with a float literal or read the value withgetDouble()before comparing.
In a real test suite
Deeply nested responses are where rootPath() and response specifications pay off. Many teams also add hasKey() checks on nested objects, for example that specs still has a color key, which catches renamed fields without pinning exact values.
Related lessons
- Extract Nested ValuesRead the same nested values into Java variables with
jsonPath(). - Nested POJO DeserializationMap the nested product onto Java classes with inner objects.
- Product Schema ValidationValidate the product's whole structure with a JSON Schema.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /products/1 and validate:
// "name" equals "Laptop Pro"
// "category.name" equals "electronics"
// "specs.color" equals "silver"
// "specs.inStock" equals true
// TODO: Print product, category, and in-stock status
}
}