Ignore missing properties during Jackson JSON deserialization in Java

JavaJsonSerializationJackson

Java Problem Overview


In the example

class Person {
   String name;
   int age;
}

If the JSON object has a missing property 'age',

{
    "name": "John"
}
Person person = objectMapper.readValue(jsonFileReader, Person.class);

it throws a JsonMappingException saying it cannot deserialize. Is there an annotation to ignore missing fields during deserialization?

Java Solutions


Solution 1 - Java

@JsonIgnoreProperties(ignoreUnknown = true) on the class level worked for me.

Solution 2 - Java

I think what you want is

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Person {
  ...
}

that's the Jackson 1.x way. I think there's a new way in 2.x. Something like

@JsonInclude(Include.NON_NULL)
public class Person {
  ...
}

These will tell Jackson to only serialize values that are not null, and don't complain when deserializing a missing value. I think it will just set it to the Java default.

Solution 3 - Java

Annotation based approach is a better way for ignoring but If needed. The manual way of deserialization:

ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Person       person = mapper.readValue(jsonFileReader, Person.class);

Solution 4 - Java

Modern versions (2.9.6) of Jackson libraries will ignore missing creator properties by default. However, if the ObjectMapper configuration is set to:

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);

Then you'll get a deserialization error if one of the properties is missing.

Solution 5 - Java

You could also change your class to use an Integer instead of an int, in which case Jackson will handle null/missing "age" values properly. Just adding this answer for those looking for an alternative that doesn't involve using annotations.

class Person {
   String name;
   Integer age;
}

Solution 6 - Java

if you annotate age with @JsonProperty(access = JsonProperty.Access.READ_ONLY) it will be ignored during deserialization only.

Solution 7 - Java

In Deserialization, two things may happen.

we have a new field in JSON String, not in Java Bean, that will be an unknown property for the object mapper. To ignore the unknown property, the object mapper configuration needs to be configured as below

ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

If a field is not in JSON String but in Java Bean, then that field will be treated as a missing property by the object mapper. To not fail on missing property, the object mapper configuration needs to be configured as below but this is the default behavior of Modern versions (2.9.6) of Jackson libraries.

ObjectMapper().configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser379151View Question on Stackoverflow
Solution 1 - JavaGustavo MatiasView Answer on Stackoverflow
Solution 2 - JavapedorroView Answer on Stackoverflow
Solution 3 - JavaFırat KÜÇÜKView Answer on Stackoverflow
Solution 4 - JavaMoisésView Answer on Stackoverflow
Solution 5 - JavambonnessView Answer on Stackoverflow
Solution 6 - JavaabdelView Answer on Stackoverflow
Solution 7 - JavaRCvaramView Answer on Stackoverflow