About this lesson
REST Assured nested POJO deserialization: objects inside objects
Learn REST AssuredAdvancedSerialization and Deserialization
How it works
Real payloads are rarely flat. A testkru product has a category object with id and name, and a specs object with weight, color and inStock. To map that, mirror the structure in Java: one class per JSON object, and a field of that class type where the nested object appears. Jackson recurses on its own, so a single as() call with Product.class fills the product, its category and its specs.
Types come from your declarations, not from the JSON. Because price is a double field, the value prints as 1299.99. That differs from jsonPath().get(), which hands back a Float for the same number. The inStock value is a JSON boolean and lands in a boolean field, and weight is a string that already includes its unit.
Every nested class follows the strict rule too. The starter Specs class declares color even though the task never prints it, and that is deliberate: remove it and the color key in the response has no field, so the whole conversion throws.
About the assignment
Convert product 1 into the provided Product class, including its nested category and specs, and print five facts from the object graph.
What you will do
- GET
/products/1and callas()withProduct.class. - Print the product name after
Product:and the price afterPrice:. - Follow the nested fields: print the category name after
Category:and the weight from specs afterWeight:. - Print the stock flag from specs after
In Stock:.
Common mistakes
Declaring category as a String
In the JSON,
categoryis an object, so Jackson refuses to put it into aStringfield. Model nested objects as classes and reach their values through the extra dot, such as the category's name.Deleting fields the test does not print
Trimming unused fields such as
coloror the category id looks tidy but breaks the mapping, because their keys still arrive. Keep every key that the API sends, or configure the mapper to ignore unknown ones.Printing the nested object itself
Printing the
categoryfield shows the default object text with a class name and hash, not the category. Print the field inside it.
In a real test suite
Shared nested models such as an address or a money amount are written once and reused across many responses. Typed models also make deep assertions short: comparing a whole Specs object, for example, instead of checking three paths one by one.
Related lessons
- Deserialize Response to POJOIntroduces as() on a flat user object.
- Validate Nested JSONValidates the same kind of nested JSON with dot paths.
- Extract Nested ValuesReads nested values, including a boolean, with JsonPath.
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 {
static class Category {
public int id;
public String name;
}
static class Specs {
public String weight;
public String color;
public boolean inStock;
}
static class Product {
public int id;
public String name;
public double price;
public Category category;
public Specs specs;
}
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /products/1 and deserialize to Product
// TODO: Print name, price, category.name, specs.weight, specs.inStock
}
}