Generate Java POJOs from nested JSON for Spring Boot

Turn a nested JSON sample into Java POJOs or records, preserve JSON names with Jackson, and review nullable and inferred fields.

Open JSON to Java

Start with a representative API response

An order response contains an embedded customer and a list of items. A DTO should represent that structure, including fields that are absent in some items. Paste this sample into JSON to Java, name the root OrderResponse, set package com.example.model, choose POJO, and enable Jackson annotations.

{"order-id":42,"customer":{"name":"Ada"},"items":[{"sku":"A1","quantity":2},{"sku":"B2","price":4.5}]}

Inspect the generated model

The downloaded OrderResponse.java contains one public root type and static nested types, so it can live in one file. The hyphenated key becomes order_id while @JsonProperty retains the wire name. All item samples contribute fields; quantity and price can be null because each is missing in one item. The relevant output is:

package com.example.model;

import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;

public class OrderResponse {
  @JsonProperty("order-id") public Integer order_id;
  @JsonProperty("customer") public Customer customer;
  @JsonProperty("items") public List<ItemsItem> items;

  public static class Customer {
    @JsonProperty("name") public String name;
  }
  public static class ItemsItem {
    @JsonProperty("sku") public String sku;
    @JsonProperty("quantity") public Integer quantity;
    @JsonProperty("price") public Double price;
  }
}

Use the model in your application

Place the file under src/main/java/com/example/model. For a Spring Boot application configured with Jackson 2, use its configured ObjectMapper to deserialize the response. Jackson annotations require jackson-annotations and parsing requires jackson-databind; use versions managed by your project. The example below belongs inside a method with a configured mapper and the sample JSON string. Jackson 3 uses a different ObjectMapper package; follow your project's installed version.

import com.example.model.OrderResponse;
import com.fasterxml.jackson.databind.ObjectMapper;

// Inside a method declaring throws java.io.IOException:
OrderResponse order = mapper.readValue(json, OrderResponse.class);
System.out.println(order.customer.name); // Ada
System.out.println(order.items.get(1).quantity); // null

Choose records or Lombok deliberately

POJOs use public fields and need Java 8+. Optional Lombok @Data adds generated methods and requires Lombok plus annotation processing. Records require Java 17+ and expose accessors such as customer().name(); use a Jackson version supporting records (Jackson 2.12+). Lombok @Data is disabled for record output. Start with dependency-free POJOs if you do not need the optional features.

Review inference before using the DTO

Null-only fields infer Object; empty lists infer List<Object>; mixed incompatible list elements infer Object. Missing fields use nullable boxed types, not validation constraints. Integers become Integer or Long, decimals become Double. JavaScript parsing can round decimal values: for money, inspect the original API schema and use BigDecimal deliberately, or send decimal strings. Unsafe integer samples are rejected. The generator accepts objects or non-empty arrays of objects, with 1,000,000 characters, 40-level and 200-type limits. It does not infer a complete schema, validate business rules, or resolve every future API response.

Try these examples in JSON to Java

API references

Browse all tutorials