JSON to Java Class

Infer Java records, ready for Jackson, from a JSON sample.

About JSON to Java Class

Generate Java records from a JSON sample, with List types, boxed types for values that can be null and Jackson annotations where a key needs one.

How to use

  1. Paste JSON, or load a .json file (up to 5 MB). The root must be an object or an array of objects.
  2. Optionally change the root record name (default Root). It must start with a letter or underscore and contain only letters, digits and underscores.
  3. The Java code updates as you type. Copy it, or Download it as a .java file named after the root record. Records need Java 16 or later, and @JsonProperty needs Jackson on the classpath.

What it does

JSON Java
string String
integer long
other number double
boolean boolean
null, or mixed types Object
array List<T>, with boxed elements such as List<Long>
nested object a named record, RootAddress
null in some items, or key missing from some items the boxed type: Long, Double, Boolean
  • Component names are camelCase. @JsonProperty("key") is added only when the name differs from the JSON key: user_name becomes userName, a key that starts with a digit gets an underscore (2fa becomes _2fa) and a reserved word gets one at the end (class becomes class_).
  • Only the root record is public. Nested records are package-private and listed first, so the whole output compiles as one file; move a record to its own file to make it public.
  • Imports cover only what is used: java.util.List and Jackson's JsonProperty.
  • Two keys that would get the same name, such as first-name and first_name, are numbered: firstName, firstName2.
  • It does not generate classes with getters and setters, Lombok or Gson annotations, or date types: date strings stay String. Integers are always long, which holds values up to 2^63 - 1; change a component to BigInteger by hand if a value is larger.

Example

{ "id": 1, "user_name": "Ada", "tags": ["admin"] }
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public record Root(
    long id,
    @JsonProperty("user_name") String userName,
    List<String> tags
) {}

Privacy

Inference runs in the page. No request is made and nothing is stored.

Frequently asked questions

Should I use a record or a class for JSON in Java?
A record suits data that is read and passed on: it is immutable, and the compiler writes the constructor, accessors, equals, hashCode and toString. Use a class when the object must be mutable or the project runs on a Java version older than 16.
Does Jackson support Java records?
Yes, from Jackson 2.12. ObjectMapper.readValue(json, Root.class) calls the canonical constructor of the record and matches JSON keys to component names, or to the name given in @JsonProperty.
When do I need @JsonProperty?
When the JSON key is not the Java name: keys in snake_case or kebab-case, keys that start with a digit and keys that are reserved words such as class. Where the key already matches the component name the annotation adds nothing.
Why are JSON integers long and Long instead of int?
A JSON integer has no declared size, and IDs and timestamps often exceed the int limit of about 2.1 billion. The primitive long cannot be null, so a value that is null or missing somewhere in the sample uses the boxed Long.