In this quick tutorial, we will discuss how to use Jackson provided @JsonInclude annotation to ignore null or empty fields in a JSON.
@JsonInclude annotation provides Include.NON_NULL attribute to ignore fields with Null values and Include.NON_EMPTY attribute to ignore fields with Empty values.
By default, Jackson does not ignore Null and Empty fields while writing JSON. We can configure Include.NON_NULL and Include.NON_EMPTY at property level as well as at class level using @JsonInclude annotation.
Dependencies
Let’s first add the following dependencies to the pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.
Table of contents
- @JsonInclude Annotation Overview
- Using Include.NON_NULL and Include.NON_EMPTY at the Property level
- Using Include.NON_NULL and Include.NON_EMPTY at the Class level
- Using Include.NON_NULL and Include.NON_EMPTY globally with ObjectMapper
1. @JsonInclude Annotation Overview
The @JsonInclude annotation contains below two values:
- Include.NON_NULL: Indicates that only properties with not null values will be included in JSON.
- Include.NON_EMPTY: Indicates that only properties that are not empty will be included in JSON.
We can configure Include.NON_NULL and Include.NON_EMPTY at property level as well as at class level using @JsonInclude annotation.
2. Using Include.NON_NULL and Include.NON_EMPTY at the Property level
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
public class Employee {
private int id;
@JsonInclude(Include.NON_NULL)
private String firstName;
@JsonInclude(Include.NON_EMPTY)
private String lastName;
public Employee(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Let's test above code with a main() method:
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonIncludeAnnotationTest {
public static void main(String[] args) throws JsonProcessingException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Employee employee = new Employee(10, null, "");
String result = mapper.writeValueAsString(employee);
System.out.println(result);
}
}
Output:
{
"id" : 10
}
3. Using Include.NON_NULL and Include.NON_EMPTY at the Class level
Let's add @JsonInclude(Include.NON_NULL) at class level:
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@JsonInclude(Include.NON_NULL)
public class Employee {
private int id;
private String firstName;
@JsonInclude(Include.NON_EMPTY)
private String lastName;
public Employee(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Let's test above code with a main() method:
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonIncludeAnnotationTest {
public static void main(String[] args) throws JsonProcessingException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Employee employee = new Employee(10, null, "Fadatare");
String result = mapper.writeValueAsString(employee);
System.out.println(result);
}
}
Output:
{
"id" : 10,
"lastName" : "Fadatare"
}
4. Using Include.NON_NULL and Include.NON_EMPTY globally with ObjectMapper
Jackson also allows configuring this behavior globally on the ObjectMapper:
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);
Here is the complete code:
package net.javaguides.jackson.annotations;
public class Employee {
private int id;
private String firstName;
private String lastName;
public Employee(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Let's configure Include.NON_NULL and Include.NON_EMPTY globally with ObjectMapper and tested here at:
package net.javaguides.jackson.annotations;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonIncludeAnnotationTest {
public static void main(String[] args) throws JsonProcessingException {
// Create ObjectMapper object.
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Employee employee = new Employee(10, null, "");
String result = mapper.writeValueAsString(employee);
System.out.println(result);
}
}
Output:
{
"id" : 10
}
Related Articles
- Change Field Name in JSON using Jackson (popular)
Comments
Post a Comment
Leave Comment