Map snake_case JSON Keys with Jackson @JsonProperty
Use Jackson's @JsonProperty annotation to bridge snake_case JSON keys with idiomatic camelCase Java field names. Learn the @JsonNaming alternative for whole-class strategies.
Detailed Explanation
Bridging Naming Conventions with @JsonProperty
Java fields use camelCase. Many JSON APIs (Stripe, GitHub, Slack, AWS) use snake_case. The Jackson @JsonProperty annotation tells the deserializer which JSON key maps to which Java field.
Example JSON
{
"user_id": 42,
"first_name": "Alice",
"last_name": "Williams",
"is_active": true
}
Generated Java with @JsonProperty
package com.example.model;
import com.fasterxml.jackson.annotation.JsonProperty;
public class User {
@JsonProperty("user_id")
private Integer userId;
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
@JsonProperty("is_active")
private Boolean isActive;
// accessors
}
The generator only emits the annotation when the JSON key actually differs from the camelCase Java field name. A key like email does not get an annotation; user_id does.
Alternative: @JsonNaming
If your entire payload uses snake_case, a single class-level annotation is more concise:
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class User {
private Integer userId;
private String firstName;
// ...
}
This applies the strategy to every field, removing the per-field annotations entirely. @JsonProperty overrides @JsonNaming on a case-by-case basis when you need exceptions.
Global Configuration
You can also configure the strategy globally on the ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
When to Use Each Approach
- Per-field @JsonProperty — when only some keys differ from camelCase, or when keys do not follow a consistent strategy.
- @JsonNaming — when the whole class uses one consistent strategy (snake_case, kebab-case, UPPER_CAMEL_CASE).
- Global ObjectMapper config — when every payload in your application uses the same convention.
Other Useful Jackson Annotations
@JsonIgnore— exclude a field from serialization@JsonInclude(NON_NULL)— drop null fields from output@JsonAlias— accept multiple incoming key names@JsonFormat— control date and number formatting
Use Case
Almost every public REST API published in the last decade uses snake_case for JSON keys. Adding @JsonProperty (or @JsonNaming) on the consuming Java POJOs is mandatory unless you want to rename your fields to look unidiomatic.