How to Ignore Unknown Properties While Parsing JSON in Java - TechDoko

Hot

Post Top Ad

How to Ignore Unknown Properties While Parsing JSON in Java

One of the common problem while parsing JSON in Java using Jackson API is that it fails when your JSON contains unknown properties i.e. your Java class doesn't have all the field corresponding to all JSON properties. For example, if you are consuming JSON from a REST Web Service and tomorrow they added a new field into JSON then your code will break because Jackson will throw UnrecognizedPropertyException and stop parsing JSON. This is troublesome and can cause problems in production if you are not aware. The code worked fine for months but it broke as soon as source system added a new field is added to REST API.

Jackson API provides two ways to ignore unknown fields, first at the class level using @JsonIgnoreProperties annotation and second at the ObjectMapper level using configure() method.

Ignoring unknown properties using @JsonIgnoreProperties

If you are creating a Model class to represent the JSON in Java, then you can annotate the class with @JsonIgnoreProperties(ignoreUnknown = true) to ignore any unknown field. Which means if there is a new field is added tomorrow on JSON which represent your Model then Jackson will not throw UnrecognizedPropertyException while parsing JSON in Java.

You can use this approach if you want to ignore unknown properties only for that Model class, but this is preferred approach because it provides you more control.

Let's see an example of using @JsonIgnoreProperties in Java:

Suppose I have following JSON, which represents a book, Java Programming and a Java model class in a project:

If tomorrow, I add a new field called "edition" in the JSON then parsing of this JSON will fail with the UnrecognizedPropertyException error. Something like :

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "edition" (class EBook), not marked as ignorable (3 known properties: , "title", "price", "author"])"

This means Jackson is not able to find any field in your EBook class for "edition" property in JSON and hence it's throwing the UnrecognizedPropertyException error.

You can solve this problem and prevent this error by using @JsonIgnoreProperties annotation as shown below:
We have just annotated a whole model class as @JsonIgnoreProperties(ignoreUnknown = true), which mean any unknown property in JSON String i.e. any property for which we don't have a corresponding field in the EBook class will be ignored. If you compile and run your program again it will work fine.

In Jackson 2.x, the @JsonIgnoreProperties reside in com.fasterxml.jackson.annotation package, hence you need to import it as :

import com.fasterxml.jackson.annotation.JsonIgnoreProperties

If you are using an older version of Jackson API e.g. Jackson 1.x then this annotation belongs to a different package, beware of that, especially if you have both Jackson 1.x and Jackson 2.x in your classpath.

Ignoring Unknown Property in JSON Globally using Jackson

Another way to deal with unknown properties in JSON you are parsing is to configure ObjectMapper not to fail when it encounters an unknown property. This will also solve the problem of UnrecognizedPropertyException. You can enable this setting by calling configure() method as shown below:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.


No comments:

Post a Comment