JSON to Java class generator
Paste a JSON response and get Java types for it in one file. Choose classic classes with private fields, getters and setters, or compact records (Java 16+). Keys that are not valid camelCase names get Jackson's @JsonProperty, so the mapping stays exact.
Runs in your browser: nothing you enter is sent anywhere.
How to use it
- Paste a JSON sample. Arrays with several objects give the best result: keys missing from some items are detected.
- Choose classes (fields, getters and setters) or records, and whether to add Jackson annotations.
- Save the output as Root.java and rename the classes to fit your project.
How it works
The sample is read into a shape: each key with every type seen at that place. All objects of one array are merged into one class.
- Field names are camelCase: order_id → orderId, gift_note → giftNote. Getters follow JavaBeans: getOrderId(), and isPaid() for a primitive boolean.
- Class names come from keys: items holds a List
- , customer becomes Customer.
- Imports include only what is used: java.util.List and com.fasterxml.jackson.annotation.JsonProperty.
- With a top-level array, a comment names the type to read it into, such as List.
Worked example
{"items":[{"sku":"A","note":null,"qty":1},{"sku":"B","note":"x"}]} as records gives:
import java.util.List;
public record Root(
List<Item> items
) {
public record Item(
String sku,
String note,
Long qty
) {}
}
qty is missing from the second item, so it is a boxed Long that can be null.
Tips
- Read it with Jackson:
new ObjectMapper().readValue(json, Root.class).
- Add
@JsonIgnoreProperties(ignoreUnknown = true) if the real data may carry keys the sample did not have.
- Check numeric types: a price that was always whole in the sample comes out as long.
Frequently asked questions
How are JSON types mapped to Java?
Strings become String, true and false boolean, whole numbers long and other numbers double (a mix of both becomes double). Arrays become List<T>, objects become their own classes, and a place holding different kinds of values becomes Object.
When does it use Long instead of long?
A primitive cannot hold null. When the sample has null for a number or boolean, or the key is missing from some objects of an array, the boxed type is used: Long, Double, Boolean. Inside List<...> the boxed type is always used, as Java generics require.
Why are the other classes nested inside Root?
Java allows one public top-level class per file. Putting the others inside Root as public static classes (or nested records) makes the output one file, Root.java, that compiles as it is. Refer to them as Root.Item, or move them to their own files.
What happens to keys like first-name or class?
Field names are camelCase built from the key: first-name becomes firstName, and a Java keyword gets an underscore, class_. With Jackson on, such fields get @JsonProperty("first-name"); with it off, a comment keeps the original key.
Do records work with Jackson?
Yes, Jackson 2.12 and newer deserializes records through their canonical constructor, and @JsonProperty on a record component works the same as on a field.