How to ignore null fields with Jackson
In Jackson, we can use @JsonInclude(JsonInclude.Include.NON_NULL)
to ignore the null
fields during serialization.
Table of contents:
- 1. Jackson default include null fields
- 2. Ignore all null fields globally
- 3. Ignore null fields (Class Level)
- 4. Ignore null fields (Field Level)
- 5. Download Source Code
- 6. References
P.S Tested with Jackson 2.17.0
1. Jackson default include null fields
In Jackson, the default is includes all the null fields during serialization.
Staff.java
public class Staff {
private String name;
private int age;
private String[] position;
private List<String> skills;
private Map<String, BigDecimal> salary;
private boolean active;
// getters, setters, constructors and etc
package com.mkyong.json.jackson.tips;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Staff;
public class IgnoreNullFieldExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
Staff staff = new Staff();
staff.setName("mkyong");
staff.setAge(42);
try {
String jsonOutput = mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(staff);
System.out.println(jsonOutput);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Output
{
"name" : "mkyong",
"age" : 42,
"position" : null,
"skills" : null,
"salary" : null,
"active" : false
}
2. Ignore all null fields globally
We can configure the ObjectMapper
to ignore all null fields globally:
IgnoreNullFieldExample
package com.mkyong.json.jackson.tips;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.Staff;
public class IgnoreNullFieldExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
// ignore null fields
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Staff staff = new Staff();
staff.setName("mkyong");
staff.setAge(42);
try {
String jsonOutput = mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(staff);
System.out.println(jsonOutput);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
Output
{
"name" : "mkyong",
"age" : 42,
"active" : false
}
3. Ignore null fields (Class Level)
We can put the @JsonInclude(JsonInclude.Include.NON_NULL)
annotation on the class level to tell Jackson to ignore all null fields during serialization.
Staff.java
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Staff {
private String name;
private int age;
private String[] position;
private List<String> skills;
private Map<String, BigDecimal> salary;
private boolean active;
// getters, setters, constructors and etc
Output
{
"name" : "mkyong",
"age" : 42,
"active" : false
}
4. Ignore null fields (Field Level)
We can put the @JsonInclude(JsonInclude.Include.NON_NULL)
annotation on a specified field to tell Jackson to ignore it if it contains a null value during serialization.
Staff.java
import com.fasterxml.jackson.annotation.JsonInclude;
public class Staff {
private String name;
private int age;
private String[] position;
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<String> skills;
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, BigDecimal> salary;
private boolean active;
// getters, setters, constructors and etc
Output
{
"name" : "mkyong",
"age" : 42,
"position" : null,
"active" : false
}
5. Download Source Code
$ git clone https://github.com/mkyong/java-json
6. References
About Author
Comments
Subscribe
0 Comments