Skip to content
CodeKruPro

Validate Nested JSON

11/62

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

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

  1. Request /products/1 and move to then().
  2. Check the top-level name and the nested category.name with equalTo().
  3. Check specs.color and specs.inStock, the latter against a boolean, not a string.
  4. Print the three labelled lines from the task, the colour check has no output line.

Common mistakes

  • Quoting the boolean

    Matching specs.inStock against the string true fails, and the message shows the actual value in angle brackets because it is a Boolean. Pass the boolean literal instead.

  • Expecting an error for a bad parent key

    A path like spec.color does not complain that spec is missing, it just yields null. Read the failure message carefully to find which segment is wrong.

  • Asserting the price with a double

    equalTo() with 1299.99 fails against the parsed Float. Compare with a float literal or read the value with getDouble() 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.

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.

Main.java
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
    }
}