Explain Codes LogoExplain Codes Logo

Ignoring new fields on JSON objects using Jackson

java
json-serialization
jackson-annotations
deserialization
Anton ShumikhinbyAnton Shumikhin·Feb 23, 2025
TLDR

Handle unexpected JSON fields in Jackson by setting the DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES to false in your ObjectMapper.

new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // The configuration equivalent of "expect the unexpected". Well, ignoring it, more like.

For specific classes, apply the @JsonIgnoreProperties(ignoreUnknown = true) annotation:

@JsonIgnoreProperties(ignoreUnknown = true) public class MyEntity {} // Doors locked, blinds pulled. Unexpected guests? Not on my watch.

Place this snippet on top of your POJO and voila - unexpected fields in your JSON are seamlessly ignored.

Unveiling the versatility of Jackson

Handling unknown fields with grace

In an ever-evolving application, JSON contracts might undergo change, ushering in new fields. Jackson offers methods to adapt smoothly, ensuring your deserialization mechanism doesn't snap with changing JSON data.

Embrace DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES set to false for universal handling of unknown JSON fields, eliminating the need to adjust each POJO.

Mastering the mix - Global and local strategies

While the global configuration is handy, sometimes you require specificity at the class level. Melding @JsonIgnoreProperties(ignoreUnknown = true) with a global switch gives an efficient equilibrium between broad and finely-tuned control.

Ensuring compatibility across Jackson versions

Make sure to use the proper imports according to your Jackson version. For Jackson 2.0 and higher, use:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

For prior versions (1.x), you'll need:

import org.codehaus.jackson.annotate.JsonIgnoreProperties; // Going retro? No problem, Jackson's got your back.

Annotations vs Global configurations - Pick your poison

You might be tempted to use the global configuration, but applying @JsonIgnoreProperties(ignoreUnknown = true) at class level provides a safety net for specific scenarios where you wish to be sure of ignoring unknown fields.

Understanding the landscape

Peruse the @JsonIgnoreProperties documentation for a thorough dive. Comprehending DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES in depth will give insights into its potential usefulness.

Tips for different Jackson versions

For Jackson 2.X, this is how you go about it:

ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // Code that gracefully says "New JSON fields? Cool story, bro."

For Jackson 1.X:

ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Adopting these versions-specific configurations shields your application from potential crashes due to changes in JSON structures.