Parsing JSON into Java types is a daily task for backend developers. With Java 17 records now part of the language, immutable data models are easier to write and maintain. But the truth is: most tutorials still show you how to map JSON to POJOs, not to records, and they rarely cover tooling that saves time.
In this post, I’ll walk through converting JSON to Java 17 records using Jackson, explain annotations that matter, and share how to automate this with an online generator. I’ll also show how the Toolshref JSON to Java code converter for records fits into real workflows.
Why Use Java Records for JSON Data?
Java records are simple, immutable data carriers introduced in Java 16 and finalized in Java 17.
They provide:
Concise syntax — no boilerplate constructors, getters, equals/hashCode, toString.
Immutability by default — fields are final.
Better alignment with JSON APIs where data objects are just containers.
In APIs built on Spring Boot, Quarkus, or Micronaut, records reduce clutter and keep focus on business logic. But developers often ask:
“Can Jackson deserialize JSON into a Java record?”
Yes — but with a couple of rules you need to know.
Jackson JSON to Record: What Works Out of the Box
Jackson 2.12+ supports Java records natively. That means:
Jackson will map JSON properties to record components.
You don’t need setters.
The canonical constructor can receive values directly.
Example JSON:
{
"id": 101,
"name": "Alice",
"email": "alice@example.com"
}
Equivalent Java record:
public record User(int id, String name, String email) {}
And Jackson deserialization:
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(jsonString, User.class);
That’s it. If JSON keys match record component names, Jackson will wire them correctly.
When You Need Jackson Annotations for Records
There are cases where defaults aren’t enough:
JSON uses snake_case but Java uses camelCase.
- You want to ignore unknown properties.
- You need custom date/time formats.
- Jackson annotations still apply to records.
Here’s how to handle common variations.
@JsonProperty
Use @JsonProperty when JSON keys don’t match record field names:
public record User(
@JsonProperty("user_id") int id,
@JsonProperty("full_name") String name
) {}
This tells Jackson exactly how to map.
**@JsonIgnoreProperties
**If your input JSON has extra fields you don’t care about:
@JsonIgnoreProperties(ignoreUnknown = true)
public record User(int id, String name) {}
This prevents exceptions on unrecognized fields.
@JsonFormat
Useful for dates:
public record Event(
int id,
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
LocalDateTime timestamp
) {}
Common Jackson Pitfalls With Records
**1. Missing No-Arg Constructor
**Records don’t have a no-arg constructor, which breaks older frameworks expecting one. But Jackson doesn’t need it if the canonical constructor matches all components.
If you need more control, use @JsonCreator:
public record User(
int id,
String name
) {
@JsonCreator
public User(@JsonProperty("id") int id,
@JsonProperty("name") String name) {
this.id = id;
this.name = name;
}
}
This is the same as the default constructor Jackson uses, but explicit.
**2. Nested JSON Objects
**Jackson handles nested structures automatically as long as corresponding Java types exist.
Example:
{
"id": 5,
"profile": {
"age": 30,
"status": "active"
}
}
Java records:
public record Profile(int age, String status) {}
public record User(int id, Profile profile) {}
Jackson will resolve nested fields without extra code.
Use the Toolshref JSON to Java Record Converter (Hands-On)
I built and use this tool regularly when mapping API responses to Java models. It’s straightforward and avoids the manual pain.
How It Works
Paste your JSON.
Choose “Record”.
Get ready-to-use Java 17 record classes.
Example output for the earlier JSON:
public record User(int id, String name, String email) {}
For nested JSON, it generates:
Separate record files
Proper nested types
Instead of hand-coding nested records, the tool does it instantly.
Why This Matters
In teams where backend APIs evolve quickly, keeping models in sync is a chore. Using an online JSON to record converter:
Reduces human errors
Matches naming consistently
Outputs code you can refine
This helps particularly when integrating third-party APIs or prototyping.
Top comments (0)