How to Ignore Empty and Null Fields using Jackson .

  •  Jackson provides Include.NON_NULL to ignore fields with Null values and Include.NON_EMPTY 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.
  • To configure Include.NON_NULL and Include.NON_EMPTY globally for every class we need to do it at ObjectMapper level by using its  setSerializationInclusion() method.
  • Use Include.NON_NULL and Include.NON_EMPTY at property level with @JsonInclude annotation
  • Use ObjectMapper to ignore Null and Empty fields globally.
  • Include.NON_NULindicates 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. Non-empty can have different meaning for different objects such as List with size zero will be considered as empty. In case of Map to check empty isEmpty() will be considered as empty.

1. Using Include.NON_EMPTY and Include.NON_NULL at Property level

Find below example.

public class Student{
    @JsonInclude(Include.NON_NULL)		
    private String name;
    @JsonInclude(Include.NON_EMPTY)	
    private String phone;
    ------
    ------
}

If  value of name is null then it will not be included while writing JSON for student object. same like if value of phone is empty then it will not be included while writing JSON for student object.

2. Using Include.NON_NULL and Include.NON_EMPTY at Class level

Find below example for Include.NON_NULL at class level.

@JsonInclude(Include.NON_NULL)
public class student{
  ------
}

Using Include.NON_NULL at class level means any property of this class having null value will not be included in JSON.
find below example for Include.NON_EMPTY at class level.

@JsonInclude(Include.NON_EMPTY)	
public class Student{
  ------
}

Using Include.NON_EMPTY at class level means any property of this class having empty value will not be included in JSON.

3. Using Include.NON_NULL and Include.NON_EMPTY globally with ObjectMapper

Find  below example.

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);

In the above code we have configured ObjectMapper with Include.NON_NULL and Include.NON_EMPTY using setSerializationInclusion() that ignore Null and Empty values globally for every class. Now when we write JSON using mapper instance for any given object, then the properties of that object having null or empty value will not be included in JSON.

 

Example of Include.NON_NULL and Include.NON_EMPTY Globally with ObjectMapper

Find the example of Include.NON_NULL and Include.NON_EMPTY with ObjectMapper
Book.java

package com.web.model;
public class Student{
	private int student_id; 
	private String student_name;
  	private String student_phone;
  	private String student_address;   
  	public student_address(){}
  	public Student(int student_id, String student_name, String student_phone, String student_address) {
  		this.student_id= student_id;
  		this.student_name= student_name;
  		this.student_phone= student_phone;
  		this.student_address= student_address;
  	}
	//setter and getter
}

IgnoreNullEmptyValueWithObjectMapper .java

package com.web;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class IgnoreNullEmptyValueWithObjectMapper {
  public static void main(String[] args) throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);
    mapper.setSerializationInclusion(Include.NON_EMPTY);
    Student student= new Student (1000, "Himanshu", "", null);
    String studentJson= mapper.writerWithDefaultPrettyPrinter()
		 .writeValueAsString(student);
    System.out.println(studentJson);
  }
}

Output

{
  "student_id" : 101,
  "student_name" : "Abhishek"
}

Without Include.NON_NULL and Include.NON_EMPTY, output will be as following.

{
  "student_id" : 101,
  "student_name" : "Spring",
  "student_phone" : "",
  "student_address" : null
 }

Leave a comment