Skip to content
CodeKruPro

Nested POJO Deserialization

47/62

Nested POJO Deserialization

For complex JSON with nested objects, create matching nested POJOs. Every key at every level needs a field (Jackson rejects unknown keys), so the starter's Specs class declares color even though you do not print it:

// JSON: {"category": {"id": 1, "name": "electronics"}}
static class Category { public int id; public String name; }
static class Product { public Category category; ... }

Task: Deserialize product 1 with its nested category and specs objects.

Expected output:

Product: Laptop Pro
Price: 1299.99
Category: electronics
Weight: 1.5kg
In Stock: true

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

  1. GET /products/1 and call as() with Product.class.
  2. Print the product name after Product: and the price after Price:.
  3. Follow the nested fields: print the category name after Category: and the weight from specs after Weight:.
  4. Print the stock flag from specs after In Stock:.

Common mistakes

  • Declaring category as a String

    In the JSON, category is an object, so Jackson refuses to put it into a String field. 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 color or 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 category field 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.

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 {

    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
    }
}