Deserializing JSON with Unknown Properties Jackson
In this section, we will cover the most important unmarshalling process by using Jackson's "2.x" version. At present, the JSON has become popular for representing data. While we deal with the JSON data having unknown properties, and unmarshal such JSON, it will throw an Unknown Property exception.
Let's understand how we can unmarshal JSON data with Additional/Unknown fields with an example.
Deserializing JSON with addition or unknown fields
At the time of unmarshalling the JSON, JSON data can be of any shape and size. When we map it to the predefined Java objects with a set number of fields, it may cause an exception. The exception may occur when it doesn't find the matching field in the Java object. In order to handle this exception, we need to ignore the JSON properties that cannot be mapped to an existing Java field.
When we deserialize the JSON to the Product object, it will lead to the com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.
Now, in order to deal with the unknown fields, we will configure the full ObjectMapper class for ignoring the unknown properties in the JSON. We configure the ObjectMapper class in the following way:
Now, we simply configure the ObjectMapper class and add it to the above code.
There is one more way of dealing with unknown properties, i.e., by using @JsonIgnoreProperties(ignoreUnknown = true) annotation at the class level in the following way:
When we use annotation at the class level, there is no need to configure ObjectMapper class for ignoring JSON properties.