
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: July 18, 2024
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.
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>
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.
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());
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());
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.
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.