Skip to content
CodeKruPro

Extract Nested Values

13/62

Extract Nested Values

Use dot notation with jsonPath() to extract nested values:

response.jsonPath().getString("address.city")  // String inside a nested object
response.jsonPath().getBoolean("settings.active")  // nested boolean

Task: GET /products/1 and extract:

  • The category name (nested in category.name)
  • The weight (nested in specs.weight)
  • The inStock flag (nested in specs.inStock)

Expected output:

Category: electronics
Weight: 1.5kg
In Stock: true

About this lesson

REST Assured: extract nested JSON values with getString and getBoolean

Learn REST AssuredIntermediateJSON Path Extraction

How it works

Dot paths work with jsonPath() exactly as they do in body(). getString("category.name") walks into the category object, and getBoolean("specs.inStock") returns a primitive boolean ready for an if statement or an assertion.

The typed getters each convert in their own way. getString() turns anything into text, so a boolean comes back as the word true and a whole object comes back as a Groovy-style map string in square brackets. getMap() returns the object as a Java Map instead. getInt() on text such as a weight with a unit throws a NumberFormatException.

If you plan many reads under one parent, setRootPath() on a JsonPath sets a prefix so later paths can be shorter.

About the assignment

Product 1 hides its category name, weight and stock flag one level down, in two different objects. You request it and read those three nested values into Java, printing each with its label.

What you will do

  1. Send a GET to /products/1 and keep the Response.
  2. Read category.name and specs.weight with getString().
  3. Read specs.inStock with getBoolean().
  4. Print the three values labelled Category:, Weight: and In Stock:.

Common mistakes

  • getBoolean on a misspelt path

    A wrong key gives getBoolean() nothing to unbox, so it throws a NullPointerException rather than returning false. The stack trace points at REST Assured internals, not at your typo.

  • getBoolean on a non-boolean field

    Asking for specs.color with getBoolean() does not fail. It quietly returns false, so a wrong path can make a stock check look like an out-of-stock product.

  • Parsing units as numbers

    The weight is text with a unit, so getInt() or getDouble() on it throws NumberFormatException. Read it as a string and parse the numeric part yourself if you need it.

In a real test suite

Reading nested values into variables is useful when one test needs to make decisions, such as skipping a purchase step for a product that is out of stock. When the same nested shape appears in many tests, a POJO with inner classes keeps that structure in one place.

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.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /products/1
        // TODO: Extract category.name, specs.weight, specs.inStock
        // TODO: Print each value
    }
}