Create Java model classes from a JSON response
JSON to Java infers Java model code for an observed JSON structure. It is a starting point for mapping API responses into a typed application.
When should you use it?
- Prototype models for a Java API client.
- Inspect nested object structure as classes.
- Create a draft DTO from a test response.
Example: infer Java fields from JSON
This abbreviated example shows the inferred name and active fields in Java. The full generated file includes imports and helpers and may change with generator versions; review it before adding it to a project.
{"name":"Alex","active":true}public class Root {
private String name;
private boolean active;
// getters and setters follow
}Review the generated model with its serializer
A Java model and its JSON binding library work together. Property annotations, accessors, imports, and conversion helpers can be as important as the field declarations. Keep the complete generated file when testing the result, then adapt its organization to your project. An abbreviated example on this page illustrates the shape rather than a standalone Java application.
Check nullable numbers and domain types
A primitive numeric field cannot represent null, while a wrapper type can. A sample that always includes a value does not prove that the API always supplies it. Review optionality, integer range, decimal precision, and date handling against the contract. Money and identifiers may need deliberate domain types rather than whatever the first example happens to suggest.
How do I generate Java DTO classes from a JSON response?
Paste a JSON object and generate Java source, then adapt the classes to your project’s package and serializer. The generated model reflects observed properties and nested types, making it useful for a first integration pass.
Illustrative Java model fields
javapublic class Root {
private String name;
private boolean active;
// Generated accessors and mapping code follow.
}The quicktype target emits additional converter code and Jackson-related imports. Review dependencies, package names, and null cases before compiling.
Will one response describe every Java field the API can return?
No. Optional or variant fields absent from the sample cannot be inferred reliably. Confirm the shape against the provider’s contract and more than one response.
What should you check before using the result?
Check package layout, naming, nullable fields, serialization library expectations, and missing cases before integration. A sample cannot establish all server variants.
Understand JSON syntaxTechnical reference: quicktype model generation documentation