In Jackson, we can use @JsonInclude(JsonInclude.Include.NON_NULL)
to ignore the null
fields.
P.S Tested with Jackson 2.9.8
1. Jackson default include null fields
1.1 Reviews a POJO, for testing later.
Staff.java
package com.favtuts.json.nullfields; import java.math.BigDecimal; import java.util.List; import java.util.Map; public class Staff { private String name; private int age; private String[] position; private List<String> skills; private Map<String, BigDecimal> salary; public Staff() { } public Staff(String name, int age) { this.name = name; this.age = age; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public String[] getPosition() { return this.position; } public void setPosition(String[] position) { this.position = position; } public List<String> getSkills() { return this.skills; } public void setSkills(List<String> skills) { this.skills = skills; } public Map<String,BigDecimal> getSalary() { return this.salary; } public void setSalary(Map<String,BigDecimal> salary) { this.salary = salary; } }
JacksonExample.java
package com.favtuts.json; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.favtuts.json.nullfields.Staff; public class JacksonIgnoreNullFields { public static void main(String[] args) { includeNullFieldsByDefault(); } static void includeNullFieldsByDefault() { ObjectMapper mapper = new ObjectMapper(); Staff staff = new Staff("favtuts", 38); try { String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff); System.out.println(json); } catch (IOException e) { e.printStackTrace(); } } }
Output
{
"name" : "favtuts",
"age" : 38,
"position" : null,
"skills" : null,
"salary" : null
}
To ignore the null
fields, put @JsonInclude
on class level or field level.
2. @JsonInclude – Class Level
Staff.java
import com.fasterxml.jackson.annotation.JsonInclude; //ignore null fields , class level @JsonInclude(JsonInclude.Include.NON_NULL) // ignore all null fields public class Staff { private String name; private int age; private String[] position; private List<String> skills; private Map<String, BigDecimal> salary; //...
Output
{
"name" : "favtuts",
"age" : 38
}
3. @JsonInclude – Field Level
Staff.java
import com.fasterxml.jackson.annotation.JsonInclude; public class Staff { private String name; private int age; @JsonInclude(JsonInclude.Include.NON_NULL) //ignore null field on this property only private String[] position; @JsonInclude(JsonInclude.Include.NON_NULL) //ignore null field on this property only private List<String> skills; private Map<String, BigDecimal> salary;
Output
{
"name" : "favtuts",
"age" : 38,
"salary" : null
}
4. ObjectMapper.setSerializationInclusion
Alternatively, we also can configure to ignore null fields globally:
JacksonExample2.java
package com.favtuts.json; import java.io.IOException; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.favtuts.json.nullfields.Staff; public class JacksonIgnoreNullFields { public static void main(String[] args) { ignoreNullFieldsGlobally(); } static void ignoreNullFieldsGlobally() { ObjectMapper mapper = new ObjectMapper(); Staff staff = new Staff("favtuts", 38); try { // ignore the null fields globally mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(staff); System.out.println(json); } catch (IOException e) { e.printStackTrace(); } } }
Output
{
"name" : "favtuts",
"age" : 38
}
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-basic/json