Jackson: Ignore null and empty fields
This article discusses options to ignore null and empty fields during serialization to JSON using Jackson 2.x.
By default, Jackson 2.x includes null and empty object fields while serializing to JSON. Sometimes, it is desirable to ignore such fields. You can do that by following any of the following two options:
- Annotate the class or the fields with @JsonInclude
- Instruct the object mapper to skip the null fields
Option 1: Annotating with @JsonInclude
Jackson 2.x provides @JsonInclude annotation that controls serialization of a class as a whole or its individual fields based on their values during serialization. It recognizes following valid values:
-
Include.NON_NULL
Indicates that only non-null properties should be serialized.
-
Include.NON_EMPTY
Indicates that only non-null and non-empty properties should be serialized. This acts as a superset of Include.NON_NULL.
-
Include.NON_DEFAULT
Indicates that only properties that have been set after the class object is created should be serialized. This ignores final and default fields during serialized. Default fields, in this context, are those that have values different than those assigned during the object creation. In most cases, this acts as superset of Include.NON_EMPTY.
-
Include.ALWAYS
Indicates that all properties should be serialized, irrespective of their values.
As a developer, you can dictate and control serialization of your classes by annotating them with @JsonInclude. It is important to note that object mapper cannot override these class level annotations. Also, it is equally important to note that these annotations are effective only if you use Jackson to serialize the objects. These annotations have no effect on other serialization libraries.
For sake of demonstrations, we use the Employee class shown below, as the model class and use it to demonstrate how to ignore null and empty fields during serialization.
Employee class
1 2 3 4 5 6 7 8 9 10 |
public class Employee { public int id; public String name; public String city; public String ssn; // assume getters and setters exist } |
Main
1 2 3 4 5 6 7 8 9 10 |
// create the employee object to be serialized to JSON Employee employee = new Employee(); employee.setId(101); employee.setName("Drona"); employee.setCity(""); ObjectMapper mapper = new ObjectMapper(); System.out.println( mapper.writeValueAsString(employee) ); |
Output
1 2 |
{ "id":101, "name":"Drona", "city":"", "ssn":null } |
Skipping null and empty fields
To skip null and empty fields, we annotate the class as shown below:
1 2 3 4 5 6 |
@JsonInclude(JsonInclude.Include.NON_EMPTY) public class Employee { . . } |
Output after annotating Employee class
1 2 |
{ "id":101, "name":"Drona" } |
Note: @JsonInclude can be used with individual fields as well, instead of the entire class. It will affect only the annotated field(s).
Option 2: Instructing the Object Mapper
The Jackson Object Mapper can take serialization option that controls the serialization process. You can set the option by calling setSerializationInclusion() and pass in the desired JsonInclude serialization option.
Note: Mapper cannot override the Jackson annotations on the class
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// create the employee object to be serialized to JSON Employee employee = new Employee(); employee.setId(101); employee.setName("Drona"); employee.setCity(""); // create the object mapper and instruct it to skip null values ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); System.out.println( mapper.writeValueAsString(employee) ); |
Output
1 2 |
{ "id":101, "name":"Drona" } |