How to use the JSON to Java generator
- Paste a JSON object, or an array of objects.
- Set the root class name and choose classes (POJOs) or records.
- Press Convert. Each class is marked with the file name it belongs in.
Everything runs in your browser. Nothing you type is sent to our servers. Your last input is remembered in this browser only.
What you get
Fields use camelCase Java names. Where the JSON key is different, for example user_name or first-name, the field gets a Jackson @JsonProperty annotation with the original key, so serialisation and deserialisation both work. Keys that are Java keywords, such as class or default, get a trailing underscore.
Types use wrapper classes (Integer, Long, Double, Boolean) rather than primitives, so a missing value can be null instead of silently becoming 0 or false. Whole numbers too large for an int become Long, and every item in an array is checked to work out the field list.
Java allows one public class per file, so each class starts with a comment such as // Order.java. Records need Java 16 or later, and Jackson 2.12 or later to deserialise them.
Reading JSON with Jackson
Jackson is the default JSON library in Spring Boot. With Gson, use @SerializedName instead of @JsonProperty.
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(json, Root.class);
List<Root> items = mapper.readValue(jsonArray, new TypeReference<List<Root>>() {});When you would use it
- Building request and response classes for a REST client.
- Creating Spring Boot DTOs from a sample payload.
- Mapping a JSON config file to typed objects.
Frequently asked questions
Why Integer rather than int?
A JSON field can be missing or null. A primitive int cannot hold null, so Jackson would quietly set it to 0. With Integer you can tell the difference.
Can I use Lombok instead of getters and setters?
Yes. Delete the getters and setters and add @Data (or @Getter and @Setter) to the class. Keep the @JsonProperty annotations on the fields.
Why is there no constructor?
Jackson needs a no-argument constructor for classes, and Java adds one automatically when you do not write any. Records get their constructor from the record header.
How are dates handled?
Dates are strings in JSON, so they are typed as String. Change the field to LocalDate, Instant or OffsetDateTime and register Jackson's JavaTimeModule if you want them parsed.