Explain Codes LogoExplain Codes Logo

Jackson overcoming underscores in favor of camel-case

java
prompt-engineering
best-practices
tools
Anton ShumikhinbyAnton Shumikhin·Feb 25, 2025
TLDR

Convert underscores (snake_case) in JSON to camelCase in Java using Jackson. You can do this by updating the ObjectMapper to use the SNAKE_CASE strategy:

new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);

You can also map single fields using the @JsonProperty annotation:

@JsonProperty("json_field") private String jsonField;

Choose which approach works best: globally for all fields or individually for precise control.

Implementing snake case to camel case with Jackson

To overcome the inconsistency between JSON and Java naming conventions, Jackson provides various strategies allowing you to focus on writing better code.

Use @JsonNaming: One annotation to rule them all

When you have many fields in a class and every field in the JSON uses underscore-based naming, it would be efficient to annotate the class with @JsonNaming. This will correctly map the JSON to your Java code:

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) public class User { private String firstName; // Look, ma! No underscores in my Java! private String lastName; // other fields... }

Global Strategy with Spring Boot

In a Spring Boot application, you can change the naming strategy for the entire application with a single line in the application.properties file:

spring.jackson.property-naming-strategy=SNAKE_CASE

"It's not a bug, it's a feature" — said Spring Boot to ObjectMapper.

Special Treatment with ObjectMapper

In case you need to give special attention to a JSON or you're within a non-Spring Boot application, you can create an ObjectMapper to handle your JSON:

@Bean public ObjectMapper jacksonObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); return mapper; }

Dealing with older Jackson versions

Before Jackson 2.7, you'd have to use CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES and between 2.7 and 2.12, it's SnakeCaseStrategy. Jackson keeps evolving just like software requirements.

Power and flexibility of Jackson

Jackson offers a wide range of tools and features that allow you to write clean, maintainable, and efficient code for handling JSON.

Personal touch with @JsonProperty

When the global strategy doesn't work, @JsonProperty comes to the scene. This annotation is particularly useful when JSON field names are not valid Java identifiers:

@JsonProperty("na_me") private String name; // "na_me"?! Seriously, JSON?

"It's okay, Jackson's got my back!" - this Java field, probably.

Fixing problems before they appear

Mapping JSON to Java is not without pitfalls. Always be aware of potential pitfalls like changes in field names in the JSON source or typos in the Java annotations.

Explore Jackson's repertoire

Jackson is more than just naming strategy. It offers flexible features like custom serializers/deserializers, ability to ignore properties, and much more to handle complex parsing needs.