Home » Java » How to tell Jackson to ignore a field during serialization if its value is null?

How to tell Jackson to ignore a field during serialization if its value is null?

Posted by: admin November 2, 2017 Leave a comment

Questions:

How can Jackson be configured to ignore a field value during serialization if that field’s value is null.

For example:

public class SomeClass {
   // what jackson annotation causes jackson to skip over this value if it is null but will 
   // serialize it otherwise 
   private String someValue; 
}
Answers:

To suppress serializing properties with null values, you can configure the ObjectMapper directly, or make use of the @JsonInclude annotation:

mapper.setSerializationInclusion(Include.NON_NULL);

or:

@JsonInclude(Include.NON_NULL)
class Foo
{
  String bar;
}

Alternatively, you could use @JsonInclude in a getter so that the attribute would be shown if the value is not null.

A more complete example is available in my answer to How to prevent null values inside a Map and null fields inside a bean from getting serialized through Jackson.

Questions:
Answers:

With Jackson > 1.9.11 and < 2.x use @JsonSerialize annotation to do that:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)

Questions:
Answers:

Just to expand on the other answers – if you need to control the omission of null values on a per-field basis, annotate the field in question (or alternatively annotate the field’s ‘getter’).

example – here only fieldOne will be ommitted from json if it is null. fieldTwo will always be included regardless of if it is null.

public class Foo {

    @JsonInclude(JsonInclude.Include.NON_NULL) 
    private String fieldOne;

    private String fieldTwo;
}

To omit all null values in the class as a default, annotate the class. Per-field/getter annotations can still be used to override this default if necessary.

example – here fieldOne and fieldTwo will be ommitted from json if they are null, respectively, because this is the default set by the class annotation. fieldThree however will override the default and will always be included, because of the annotation on the field.

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Foo {

    private String fieldOne;

    private String fieldTwo;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String fieldThree;
}

UPDATED to use Jackson 2 – for earlier versions of Jackson use

@JsonSerialize(include=JsonSerialize.Inclusion.ALWAYS) 

instead of

@JsonInclude(JsonInclude.Include.NON_NULL)

If this update is useful, please upvote ZiglioUK’s answer below, it pointed out this newer annotation long before this update!

Questions:
Answers:

In Jackson 2.x, use:

@JsonInclude(JsonInclude.Include.NON_NULL)

Questions:
Answers:

You can use the following mapper configuration:

mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);

Since 2.5 you can user:

mapper.setSerializationInclusion(Include.NON_NULL);

Questions:
Answers:

in my case

@JsonInclude(Include.NON_EMPTY)

made it work.

Questions:
Answers:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_EMPTY)

should work.

Include.NON_EMPTY indicates that property is serialized if its value is not null and not empty.
Include.NON_NULL indicates that property is serialized if its value is not null.

Questions:
Answers:

If you want to add this rule to all models in Jackson 2.6+ use:

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

Questions:
Answers:

For Jackson 2.5 use :

@JsonInclude(content=Include.NON_NULL)

Questions:
Answers:

You can set application.properties(or application.yml or something else):

jackson.default-property-inclusion=non_null

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

Questions:
Answers:

This has been troubling me for quite some time and I finally found the issue. The issue was due to a wrong import. Earlier I had been using

com.fasterxml.jackson.databind.annotation.JsonSerialize

Which had been deprecated. Just replace the import by

import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

and use it as

@JsonSerialize(include=Inclusion.NON_NULL)

Questions:
Answers:

Jackson 2.x+ use

mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);