Java Jackson Ignore Unrecognized Fields Example

1. Introduction

When working with JSON data using Jackson in Java, there might be scenarios where the JSON contains fields that are not present in our Java objects (POJOs). By default, Jackson throws an exception if it encounters such unrecognized fields. However, sometimes it's beneficial to ignore these fields and just process the ones that match our POJO. In this post, we'll explore how to configure Jackson to ignore unrecognized fields during the deserialization process.

2. Example Steps

1. Add the Jackson library dependencies to your project.

2. Create a Java class (POJO) representing our data structure.

3. Configure Jackson's ObjectMapper to ignore unrecognized fields.

4. Deserialize a JSON string that contains additional fields not present in our POJO.

5. Display the deserialized Java object to confirm correct data processing.

3. Java Jackson Ignore Unrecognized Fields Example

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

public class JacksonIgnoreFieldsExample {

    public static void main(String[] args) {
        try {
            // JSON string with an additional field "email"
            String jsonString = "{\"name\":\"Alice\",\"age\":30,\"email\":\"alice@example.com\"}";

            // Step 3: Configure ObjectMapper to ignore unrecognized fields
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            // Step 4: Deserialize JSON string to Java object
            Person person = objectMapper.readValue(jsonString, Person.class);
            System.out.println("Deserialized Java Object: " + person.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class Person {
        private String name;
        private int age;

        // Getters and setters
        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public int getAge() { return age; }
        public void setAge(int age) { this.age = age; }

        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }
    }
}

Output:

Deserialized Java Object: Person [name=Alice, age=30]

4. Step By Step Explanation

1. We have a Person class that represents our data structure with fields name and age.

2. The JSON string jsonString contains an additional field email which is not present in our Person class.

3. To prevent Jackson from throwing an exception due to this additional field, we configure our ObjectMapper instance to ignore unknown properties. This is achieved using the DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES feature set to false.

4. Using the readValue method of the ObjectMapper, we deserialize the JSON string into a Person object.

5. The output confirms that Jackson successfully deserialized the JSON string into a Person object, ignoring the email field.

This feature is particularly useful when dealing with evolving JSON data structures, ensuring that our application remains resilient to changes in the JSON data format.


Comments