How to Configure Jackson to Map Underscore JSON Keys to Camel Case Java Fields?

Question

How can I configure Jackson to deserialize JSON keys using underscores to camel case property names in Java objects?

class User {
    protected String firstName;
    protected String getFirstName() { return firstName; }
}

Answer

Jackson, a popular JSON processing library in Java, provides ways to customize the mapping of JSON keys to Java object fields. By default, Jackson expects property names to be in camel case. If your JSON keys use underscores (e.g., `first_name`), you can easily configure Jackson to handle this mapping automatically.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;

public class JacksonExample {
    public static void main(String[] args) throws Exception {
        String json = "{\"first_name\":\"John\"}";
        ObjectMapper mapper = new ObjectMapper();
        mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);
        User user = mapper.readValue(json, User.class);
        System.out.println(user.getFirstName()); // Outputs: John
    }
}

Causes

  • Many APIs return JSON data with keys formatted in snake_case (using underscores).
  • Java naming conventions typically use camelCase for variable names.

Solutions

  • Use the `PropertyNamingStrategies` class in Jackson to change how it maps names.
  • In your `ObjectMapper` configuration, set the naming strategy to `PropertyNamingStrategies.SnakeCaseStrategy` to automatically convert underscore keys to camel case.
  • Ensure your Java class fields are defined in camel case and provide corresponding getters and setters.

Common Mistakes

Mistake: Forgetting to set the property naming strategy on the ObjectMapper.

Solution: Always configure your ObjectMapper with the desired naming strategy before processing JSON.

Mistake: Using fields without proper getters or setters may lead to deserialization issues.

Solution: Ensure that the class has properly defined getters and setters for the fields being deserialized.

Helpers

  • Jackson JSON deserialization
  • map underscore keys to camel case
  • Java JSON mapping
  • PropertyNamingStrategies
  • Jackson ObjectMapper configuration

Related Questions

⦿Why Are JNI Calls in Java So Slow? Understanding the Performance Implications

Discover the reasons behind the slow performance of JNI calls in Java including JVM implementation details and performance optimization tips.

⦿How to Assert Equality Between Two Lists in JUnit Test Cases

Learn how to perform equality assertions on lists in JUnit tests. Ensure lists with identical content are considered equal.

⦿How to Correctly Remove an Integer from a List in Java?

Learn the correct methods for removing integers from a List in Java avoiding common pitfalls related to method overloading and autoboxing.

⦿Resolving java.lang.IllegalAccessError in Lombok with OpenJDK 16

Learn how to fix java.lang.IllegalAccessError related to Lombok and OpenJDK 16. Detailed solutions and code examples are provided.

⦿How to Compile Multiple Java Source Directories in a Single Maven Project

Learn how to configure Maven to compile multiple Java source directories in a single project with stepbystep guidance and examples.

⦿Why Doesn't the split() Method in Java Work with a Dot (.) as a Delimiter?

Learn why Javas String.split method fails to split using . and how to fix it in your code snippet.

⦿Understanding Why Java 8 Streams' .min() and .max() Compile with Static Methods from Integer

Explore why Java 8 Streams .min and .max compile when using static methods from Integer instead of a Comparator. Learn the details here.

⦿How to Handle Exceptions Thrown by a Thread in Java

Learn how to catch exceptions thrown from a thread in Java including code examples and common mistakes to avoid.

⦿How to Configure java.library.path for an Eclipse Project

Learn how to set the java.library.path in Eclipse for seamless integration of Java libraries that depend on OSspecific files like .dll .so or .jnilib.

⦿How to Determine the Source Location of a Java Class Loaded by the ClassLoader

Learn how to programmatically find where a Java ClassLoader loads a class from including handling class loading issues.

© Copyright 2025 - CodingTechRoom.com