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
- Send a GET to
/products/1and keep theResponse. - Read
category.nameandspecs.weightwithgetString(). - Read
specs.inStockwithgetBoolean(). - Print the three values labelled
Category:,Weight:andIn Stock:.
Common mistakes
getBoolean on a misspelt path
A wrong key gives
getBoolean()nothing to unbox, so it throws aNullPointerExceptionrather than returning false. The stack trace points at REST Assured internals, not at your typo.getBoolean on a non-boolean field
Asking for
specs.colorwithgetBoolean()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()orgetDouble()on it throwsNumberFormatException. 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.
Related lessons
- Validate Nested JSONAssert the same nested fields with matchers instead of reading them.
- Extract from Books with TagsRead a nested author object and a tags array from a book.
- Nested POJO DeserializationDeserialize product 1, nested objects included, into Java classes.
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.*;
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
}
}