Skip to content
Tools

JSON to Java Record Generator

Last updated:

Paste JSON and get Java records (Java 16+) whose components match each key, with List<T> for arrays and a separate record for every nested object. Keys that are not valid Java names get a @JsonProperty annotation, so Jackson maps them back to the original key.

Runs in your browser: your JSON never leaves your device – nothing is uploaded, logged or stored on a server. How we protect your data

100% in-browserNothing is uploaded

Input

Loading editor…

Paste a JSON sample

or drop a file anywhere in this box. Everything stays in your browser.

Drop a file to load it

Output

Loading editor…

Java (record) code appears here

Results update as you type.

How to use the JSON to Java

  1. Paste a sample JSON object.
  2. Set the root type name. Output is always records; there is no getter/setter class style.
  3. Review number types: whole numbers become long, decimals double, and integers too large for long become BigInteger.
  4. 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

Jackson vs. Gson with generated classes
BehaviourJackson (ObjectMapper)Gson
How records are filledCanonical constructorCanonical constructor (2.10+)
Unknown JSON keysFail by default (FAIL_ON_UNKNOWN_PROPERTIES)Ignored
Rename a key@JsonProperty("user_id")@SerializedName("user_id")
Java recordsSupported since 2.12Supported since 2.10
Missing primitive fieldStays 0 / falseStays 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.

Related tools