How to Generate Java Records from JSON
Modeling JSON in Java is cleanest with records — concise, immutable carriers that Jackson can bind directly. Generating them from a sample gets the component names, annotations, and types right automatically. Here's how the output is built.
From JSON to records
Each JSON object becomes a record, and nested objects become their own records:
{ "id": 1, "user_name": "Ada" }
import com.fasterxml.jackson.annotation.JsonProperty;
public record Root(
int id,
@JsonProperty("user_name") String userName
) {}
Records (Java 16+) give you the constructor, accessors, equals, hashCode, and toString for free, and Jackson binds JSON straight to them.
Component names and @JsonProperty
Java convention is camelCase components. When a JSON key uses another style — user_name, first-name — the component is camel-cased and a Jackson @JsonProperty annotation maps it back to the original key:
@JsonProperty("user_name") String userName
Components that collide with Java keywords get a Value suffix so they stay valid.
Nullable fields and boxed types
A record component that must hold null can't be a primitive, so when you convert an array of objects and a key is missing from some elements, the component uses a boxed type:
Integer views
Required numbers stay primitive (int, long, double, boolean) for efficiency; optional ones become Integer, Long, Double, or Boolean. Arrays map to List<T> with boxed element types, and a null or unknown type falls back to Object.
Related
Generating types for another language? See JSON to Kotlin and JSON to Rust.
Try it
Paste a JSON response and get Java records instantly — everything runs in your browser, nothing uploaded.