How to Convert JSON to Java Classes (POJOs) in Seconds
Stop writing DTOs by hand. Learn how JSON types map to Java, how nested objects and arrays become classes, and how to handle keys that are not valid Java names.
The Problem
Every REST client in Java needs classes that mirror the JSON it receives. Writing them by hand is slow and error prone, especially for deeply nested responses.
How JSON Types Map to Java
| JSON value | Java type |
|---|---|
| "text" | String |
| 42 | int (or long if larger than 2,147,483,647) |
| 9.99 | double |
| true | boolean |
| { ... } | A new class |
| [ ... ] | List |
| null | Object |
Step by Step
Keys That Are Not Valid Java Names
APIs often use keys like first-name or class. The generator converts them to camelCase fields (firstName, class_) and adds a Jackson annotation so mapping still works:
@JsonProperty("first-name")
private String firstName;
Tips for Production Code
- Validate the sample first with the [JSON Validator](/tools/json-validator); a trailing comma will break generation.
- Use a sample where arrays are not empty, so the element type can be detected.
- Explore large responses first with the [JSON Viewer](/tools/json-viewer) to pick the part you actually need.
- With Java 16 or later, you can turn simple POJOs into records once you know the shape is stable.
Summary
Generating POJOs from a real JSON sample removes boilerplate and typos. Review the types, add validation annotations where needed, and you are ready to deserialize with Jackson or Gson.
Try our free tools
Put these tips into practice with 45+ free online tools - no signup, no login.
Browse Free Tools