How to use the JSON to Java
- Paste a sample JSON object.
- Set the root type name. Output is always records; there is no getter/setter class style.
- Review number types: whole numbers become long, decimals double, and integers too large for long become BigInteger.
- Copy the records into your package; the needed imports are generated.
Worked example
A user with an array of scores
Input
{"id":7,"name":"Ada","active":true,"scores":[90,85]}Java
import java.util.List;
public record Root(
long id,
String name,
boolean active,
List<Long> scores
) {}Collections need boxed types (List<Long>, not List<long>). Records generate the constructor and accessors (root.id(), root.name()) for you.
Jackson vs. Gson with generated classes
| Behaviour | Jackson (ObjectMapper) | Gson |
|---|---|---|
| How records are filled | Canonical constructor | Canonical constructor (2.10+) |
| Unknown JSON keys | Fail by default (FAIL_ON_UNKNOWN_PROPERTIES) | Ignored |
| Rename a key | @JsonProperty("user_id") | @SerializedName("user_id") |
| Java records | Supported since 2.12 | Supported since 2.10 |
| Missing primitive field | Stays 0 / false | Stays 0 / false |
Primitives vs. wrapper types
A primitive long cannot be null, so a missing or null id silently becomes 0. If you need to know the value was absent, use Long instead. The same applies to boolean vs Boolean and double vs Double.
For money, avoid double: change the field to BigDecimal. Jackson can read JSON numbers into BigDecimal without rounding.
Names Java will not accept
Keys such as "class", "default" or "first-name" are not valid Java identifiers. The generator renames them (class_, default_, first_name) and adds @JsonProperty with the original key so Jackson still matches. For Gson, swap that for @SerializedName.
JSON to Java FAQ
How do I convert JSON to a Java object?
With Jackson: new ObjectMapper().readValue(json, Root.class). With Gson: new Gson().fromJson(json, Root.class). Both need a class whose field names match the JSON keys, which this tool generates.
Why does Jackson throw UnrecognizedPropertyException?
The JSON has a key your class does not declare, and Jackson fails on unknown properties by default. Add the field, annotate the class with @JsonIgnoreProperties(ignoreUnknown = true), or disable FAIL_ON_UNKNOWN_PROPERTIES.
Does this generate records or classes?
Records only (Java 16+). They are a compact fit for immutable JSON data and Jackson 2.12+ and Gson 2.10+ support them. If your build is on Java 8 or 11, convert each record to a class with fields and accessors by hand.
How do I read a JSON array into a List in Java?
Jackson needs the generic type: mapper.readValue(json, new TypeReference<List<Root>>() {}). Gson uses new TypeToken<List<Root>>() {}.getType().
Do I need Lombok with the generated records?
No. Records already provide the constructor, accessors, equals, hashCode and toString that Lombok's @Data or @Value would add to a class.

