JSON to Java Class
Generate Java classes, records or Lombok @Data classes that Jackson or Gson reads a JSON sample into and writes back. Every value is typed, each key is kept, and a key that some objects lack or hold as null is boxed.
About JSON to Java Class
The objects in the JSON become Java classes, all nested in one root class, which Jackson or Gson reads the JSON into and writes back. Choose a class with a private field, a getter and a setter for each key, a record, for Java 16 and later, or a Lombok @Data class, whose getters and setters Lombok writes.
Fields are lowerCamelCase and classes UpperCamelCase, and a field whose name is not its key has the key in @JsonProperty for Jackson or @SerializedName for Gson, as in @JsonProperty("created_at"), so each key is read and written back as the JSON has it. A field named as a Java keyword has Value added, as classValue does, and a class named as a type every Java file sees, such as String, has Model added.
Every value at a place counts, not just the first. A whole number is an int, a long when one is beyond 32 bits, and a BigInteger when one is beyond 64. A number written with a fraction or an exponent, as 10.0 and 1e3 are, makes a double: Jackson and Gson read those into an int too, but a key written so can hold 12.5 in the next response, which Jackson cuts to 12 for an int and Gson refuses. The place is a BigDecimal instead when a double would change one of its numbers, as it would 3.141592653589793238.
The objects at one place, such as the items of a list, make one class, with the keys in the order first seen. A key that some objects lack, or hold as null, is boxed, as an Integer is for an int, since a primitive holds no null, and Jackson and Gson leave a primitive whose key an object lacks at 0 or false. Jackson writes a field that is null as null, so a field whose key some objects lack, and that is never null where the JSON has it, has @JsonInclude(JsonInclude.Include.NON_NULL), which leaves it out when it is null. Gson leaves out every key that is null, and a note names each null it does not write back.
A key that holds two kinds of value, such as a string and a number, or only null, is an Object for Jackson and a JsonElement for Gson, and objects that are always empty are a Map<String, Object> or a JsonObject. Objects whose class javac or the library would refuse, such as a record of more than 253 components for Jackson 3, are a Map. A note says why a field has each of these types, and names each number a reader refuses or writes back changed, and each key a reader throws on.
A name already taken gets a number, as in userId2, and the class of a list's items is singular, as Order is for orders. When the JSON is an array of objects, the root class describes them, and a note gives the call that reads the whole array.
Choose Every field to box every field, for an API that can leave any key out. The code is one file, in the package you give, with only the imports it uses, and it compiles with javac 17, with Lombok 1.18.48 for a Lombok class, with no error or warning. Everything runs in your browser.
For Kotlin, see JSON to Kotlin Data Class. For C#, try JSON to C# Class, and for a schema of the JSON itself, JSON Schema Generator.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Writing the model classes for a Spring Boot, Jakarta EE or Android app from a JSON response the API sends today.
- Choosing between a class, a record and a Lombok class for one JSON, each key kept by an annotation.
- Getting names right for keys in snake_case, keys that are Java keywords, and keys with characters a name cannot hold, such as spaces, dots and dashes.
- Seeing which keys only some objects of a list carry, as the boxed fields.
- Checking, from the notes, what Jackson or Gson refuses or writes back otherwise: numbers a double changes, nulls Gson leaves out, and keys a reader throws on.
Frequently Asked Questions
- How do JSON types map to Java?
- Strings are
String, booleansboolean, objects a class and arrays aList. Integers areint, orlongorBigIntegerwhen they need one, and fractions and exponentsdouble, orBigDecimalwhen a double would change one. A key with two kinds of value, or only null, is anObject, or aJsonElementfor Gson. - Why is a field an Integer and not an int?
- A primitive holds no null. Jackson and Gson leave one whose key is missing at 0 or false, which they write back. Jackson 3 refuses null for one, as Gson does in a record, and Jackson 2 reads it as 0 or false, as Gson does in a class. So a field some objects lack, or hold as null, is boxed, as
Integeris forint. - How are keys named?
- A field is its key in lowerCamelCase, a class UpperCamelCase, and a list's items' class singular, as
Orderis fororders. A field whose name is not its key has it in@JsonPropertyor@SerializedName, ascreated_atdoes, so the key is read and written back. A Java keyword gets Value added, as inclassValue. - Which style should I choose?
- A class with getters and setters needs nothing but Jackson or Gson. A record needs Java 16 or later, and its values cannot be changed once it is made. A Lombok
@Dataclass needs Lombok, which writes the getters and setters when javac compiles the class. - What does Jackson 2 read differently from Jackson 3?
- Jackson 3's classes are in tools.jackson, and it reads Jackson 2's annotations, so both read the code. Jackson 2 refuses a key no field has, which Jackson 3 skips, and a key that comes again once a record has a value for each component. Jackson 3 refuses JSON nested over 500 deep, where Jackson 2 reads 1,000.
- Why is an object a Map?
- An object that is always empty is a
Map<String, Object>, or aJsonObjectfor Gson, and one with more fields than a class can hold is aMapof its values, such asMap<String, Integer>. A note names each, and says why. - How many fields can a class have?
- javac compiles a record of up to 254 slots of parameters, and Jackson 3 reads one of up to 253, a
longor adoubletaking two. This tool writes a Lombok class of up to 250 fields, as javac, with Lombok, overflowed its stack at 450, and a class of up to 5,000, whose constants fit in the 65,534 a class file holds. - How are null and missing keys handled?
- Jackson writes a null field as null, so a field that some objects lack, and that is never null where the JSON has it, has
@JsonInclude(JsonInclude.Include.NON_NULL), which leaves it out when it is null. Gson leaves out every key that is null, and a note names each place whose nulls it does not write back.
Common errors and gotchas
- Typing a key that some objects lack as an
int: Jackson and Gson leave it at 0 and write 0 back. Here such a field is anInteger, which holds null. - Typing every whole number as an
int: Jackson and Gson refuse 3000000000 for one. Here a key is alongwhen one of its integers is beyond 32 bits. - Leaving out
@JsonPropertyfor keys in snake_case: Jackson 2 refuses created_at for a field createdAt, and Jackson 3 and Gson skip it, so its value is lost. Here such a field has@JsonProperty("created_at"), or@SerializedName("created_at")for Gson. - Naming a
booleanfieldisActivein a class with getters: Jackson takes its getterisActive()for a propertyactive, so Jackson 2 refuses the key isActive. Here such a class has@JsonAutoDetect, which has Jackson read the fields. - Generating from a sample where a decimal field happens to hold a whole number, such as a price of 12: the field is an
int, and Jackson cuts 12.5 to 12 for it, where Gson refuses 12.5. Use a sample with a fraction, or write 12.0, which gives adouble. - Expecting a repeated key to be read as JavaScript reads it: Jackson and Gson keep the last value, as JSON.parse does, but Jackson 2 throws on one in a record once it has a value for each component, and Gson on one in an object it reads into a Map. A note names each place.