Jackson and JSON in Java, finally learn with a coding-first approach:
>> Download the eBookMocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Overview
Field names in JSON objects can be in a variety of formats. When we want to load those into our POJOs, we may encounter a problem where the property names in our Java code donβt match the naming convention in the JSON.
In this short tutorial, weβll see how to deserialize snake case JSON to camel case fields using Jackson.
2. Install Jackson
Letβs start by adding the Jackson dependency to our pom.xml file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13</version>
</dependency>
3. Deserialize With the Defaults
Letβs consider an example User class:
public class User {
private String firstName;
private String lastName;
// standard getters and setters
}
Letβs try to load this JSON, which uses the Snake Case naming standard (lowercase names separated by _):
{
"first_name": "Jackie",
"last_name": "Chan"
}
First, we need to use ObjectMapper to deserialize this JSON:
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(JSON, User.class);
However, when we try this, we get an error:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "first_name" (class com.baeldung.jackson.snakecase.User), not marked as ignorable (2 known properties: "lastName", "firstName"])
Unfortunately, Jackson cannot exactly match the names in the JSON to the field names in User.
Next, weβll learn three ways to solve this problem.
4. Use @JsonProperty Annotation
We can use the @JsonProperty annotation on the fields of our class to map the fields to the exact names in our JSON:
public class UserWithPropertyNames {
@JsonProperty("first_name")
private String firstName;
@JsonProperty("last_name")
private String lastName;
// standard getters and setters
}
Now we can deserialize our JSON to a UserWithPropertyNames:
ObjectMapper objectMapper = new ObjectMapper();
UserWithPropertyNames user = objectMapper.readValue(JSON, UserWithPropertyNames.class);
assertEquals("Jackie", user.getFirstName());
assertEquals("Chan", user.getLastName());
5. Use @JsonNaming Annotation
Next, we can use @JsonNaming annotation on the class, and all fields will be deserialized using snake case:
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class UserWithSnakeStrategy {
private String firstName;
private String lastName;
// standard getters and setters
}
Then deserialize our JSON again:
ObjectMapper objectMapper = new ObjectMapper();
UserWithSnakeStrategy user = objectMapper.readValue(JSON, UserWithSnakeStrategy.class);
assertEquals("Jackie", user.getFirstName());
assertEquals("Chan", user.getLastName());
6. Configure the ObjectMapper
Finally, we can use the setPropertyNamingStrategy method on ObjectMapper to configure it for all serialization:
ObjectMapper objectMapper = new ObjectMapper()
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
User user = objectMapper.readValue(JSON, User.class);
assertEquals("Jackie", user.getFirstName());
assertEquals("Chan", user.getLastName());
As we see, we can now deserialize our JSON into the original User object, even though the User class doesnβt have any annotation.
We should note that there are other naming conventions (kebab case, for example), and the above solutions work with them as too.
7. Conclusion
In this article, weβve seen different ways to deserialize snake case JSON to camel case fields using Jackson.
First, we explicitly named the fields. Then we set a naming strategy on the POJO itself.
Finally, we added a global configuration to the ObjectMapper.
