Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore missing properties during Jackson JSON deserialization in Java

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?

like image 779
user379151 Avatar asked Dec 14 '13 01:12

user379151


People also ask

How do I ignore unknown properties on Jackson?

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.

How do I ignore properties in JSON?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do you tell Jackson to ignore a field during serialization?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

How do I ignore JSON property if null?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.


1 Answers

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

like image 63
Gustavo Matias Avatar answered Oct 07 '22 02:10

Gustavo Matias