Convert Nested JSON Objects to Linked Java POJOs
Generate separate Java classes for nested JSON objects. Each level of nesting becomes its own POJO, with the parent class referencing the child by type.
Detailed Explanation
Modeling Nested JSON in Java
Real-world JSON almost always contains nested objects. The generator extracts each nested object into its own class, named after the parent key in PascalCase, and the enclosing class references it via a typed field.
Example JSON
{
"id": 1,
"name": "Acme Corp",
"address": {
"street": "123 Main St",
"city": "Springfield",
"zip": "62704"
}
}
Generated Java POJOs
package com.example.model;
public class Company {
private Integer id;
private String name;
private Address address;
// getters/setters omitted for brevity
}
public class Address {
private String street;
private String city;
private String zip;
// getters/setters omitted for brevity
}
How the Generator Names Nested Classes
The class name comes from the JSON key, converted with PascalCase rules:
address→Addressshipping_address→ShippingAddressbilling-info→BillingInfocontactDetails→ContactDetails
If two nested objects would resolve to the same class name, a numeric suffix is appended automatically: Address, Address2, Address3.
Nesting Depth
There is no fixed depth limit. Three- or four-level nesting (Order → LineItem → Product → Pricing) generates four classes, each linked through typed references.
Deserialization with Jackson
ObjectMapper mapper = new ObjectMapper();
Company company = mapper.readValue(json, Company.class);
String city = company.getAddress().getCity();
Jackson walks the type graph automatically — no extra configuration is needed for nested POJOs.
Tips for Reuse
If the same logical type (e.g., Address) appears under multiple parents in the source JSON, the generator currently produces independent classes for each occurrence. After generation, you can manually consolidate them into a single shared class to avoid duplication.
Use Case
Most production REST APIs return composite resources — an order with a customer block and a list of line items, a company with an address and contact details. Mapping nested JSON into linked Java POJOs is the foundation of any non-trivial integration.