Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

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.

As always, the example code from this article can be found over on GitHub.

 

Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Jackson (eBook) (cat=Jackson)
Comments are closed on this article!