JSON to Java: generate POJO classes
Mapping a JSON API to Java usually means writing a class per object, choosing field types, and adding getters and setters. This JSON to Java generator does it for you. It analyzes the sample with quicktype and writes one POJO per distinct object shape, with private fields and JavaBean accessors. Types are chosen carefully: whole numbers become long, decimals double, arrays List<T>, and ISO date strings OffsetDateTime.
Example
{
"id": 42,
"title": "Write docs",
"done": false,
"priority": 2.5,
"tags": ["docs"],
"owner": { "name": "Ada", "email": null },
"due": "2026-10-01T09:00:00Z",
"subtasks": [
{ "title": "Outline", "estimate": 2 },
{ "title": "Draft" }
]
}
With Types only checked, the Subtask class looks like this:
package io.quicktype;
public class Subtask {
private String title;
private Long estimate;
public String getTitle() { return title; }
public void setTitle(String value) { this.title = value; }
public Long getEstimate() { return estimate; }
public void setEstimate(Long value) { this.estimate = value; }
}
Notice Long rather than long: the property is missing from one subtask, so it must be nullable, and a primitive cannot hold null. Required numeric fields such as Task.id use the primitive long. The owner.email field, which is null in the sample, is typed as Object.
Types only vs. Jackson helpers
With Types only off, the output adds everything needed to read and write JSON with Jackson:
@JsonProperty("...")annotations on accessors, so JSON keys map to the right fields even when names differ (user_nameis exposed asgetUserName()).- A
Converterclass withfromJsonString(String)andtoJsonString(Task), preconfigured with anObjectMapperand date-time parsing forOffsetDateTime. - A comment listing the Maven dependencies:
jackson-databindandjackson-datatype-jsr310.
Use the helpers for a quick standalone parser. In a Spring Boot application, which already configures Jackson, the POJOs alone are usually enough, but keep the @JsonProperty annotations when JSON keys are not camelCase.
Unknown properties
By default Jackson's ObjectMapper throws UnrecognizedPropertyException when the JSON contains a key that has no matching field, which happens as soon as an API adds a new property. If your client should tolerate additions, annotate the classes with @JsonIgnoreProperties(ignoreUnknown = true) or disable DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES on the mapper. Spring Boot's auto-configured mapper already disables it.
Type mapping
| JSON | Java |
|---|---|
| integer | long / Long if optional |
| decimal | double / Double |
| boolean | boolean / Boolean |
| ISO date-time string | OffsetDateTime |
| array | List<T> |
| object | a new class named after the key |
| null only / mixed | Object |
Tips
- Package name: the output uses
io.quicktype; replace it with your own package. - One file per class: the output marks each class with a comment like
// Subtask.java. Split them into separate files, since Java allows one public top-level class per file. - Records: on Java 16+ you can turn simple POJOs into
record Subtask(String title, Long estimate) {}; Jackson 2.12+ supports records. - Lombok: if you use Lombok, replace the accessors with
@Dataor@Getter/@Setter. - Representative samples give better types: include records where optional fields are present and absent.
Format the generated code with the Java Formatter. Have XML instead of JSON? XML to Java works the same way. Need to fix the input first? Try the JSON Formatter. All generation runs in your browser.