Jackson
serialization
ignore-empty-values
JSON
Java

Jackson: ignore null or empty fields with @JsonInclude NON_NULL and NON_EMPTY

Codemia Knowledge Hub ·

Annotate the class or field with @JsonInclude(JsonInclude.Include.NON_NULL) to make Jackson skip null values, or use Include.NON_EMPTY to also skip empty strings, empty collections and empty arrays. To apply a rule everywhere, set it on the ObjectMapper with setSerializationInclusion. Which one to choose depends on whether an empty value means something in your API or is just noise.

Skip only null fields with NON_NULL

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2
3@JsonInclude(JsonInclude.Include.NON_NULL)
4public class User {
5    public String name;
6    public String email;
7}

If email is null, it is left out of the JSON. If it is an empty string, it is still serialized: NON_NULL does not suppress empty strings or empty collections.

Skip empty values too with NON_EMPTY

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2import java.util.List;
3
4@JsonInclude(JsonInclude.Include.NON_EMPTY)
5public class Product {
6    public String productId;
7    public String description;
8    public List<String> tags;
9}

With NON_EMPTY, Jackson omits:

  • null fields
  • ""
  • empty lists
  • empty arrays

That is convenient only if the omissions match the contract your clients expect.

Set the rule globally on the ObjectMapper

Instead of annotating every class, configure the mapper once:

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2import com.fasterxml.jackson.databind.ObjectMapper;
3
4ObjectMapper mapper = new ObjectMapper();
5mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

The rule then applies to everything that mapper serializes. This suits APIs with one consistent serialization policy, but check that all your DTOs really share the same semantics first.

Override the rule on one field

When one field needs different behavior from the rest, annotate just that field. Field-level annotations override a broader class or mapper setting:

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2
3public class Profile {
4    public String id;
5
6    @JsonInclude(JsonInclude.Include.NON_NULL)
7    public String nickname;
8}

This helps when an empty string or empty collection must be preserved on some fields but not others.

Example: NON_EMPTY output

java
1import com.fasterxml.jackson.annotation.JsonInclude;
2import com.fasterxml.jackson.databind.ObjectMapper;
3import java.util.Collections;
4
5@JsonInclude(JsonInclude.Include.NON_EMPTY)
6class Product {
7    public String id = "123";
8    public String description = "";
9    public java.util.List<String> tags = Collections.emptyList();
10}
11
12public class Demo {
13    public static void main(String[] args) throws Exception {
14        ObjectMapper mapper = new ObjectMapper();
15        String json = mapper.writeValueAsString(new Product());
16        System.out.println(json);
17    }
18}

Only the non-empty data appears in the output.

Decide what a missing field means

Choose inclusion rules at the DTO or mapper boundary rather than deep inside domain objects. That keeps the serialization policy visible and stops one team's payload cleanup from changing another endpoint's meaning.

An omitted field can mean very different things:

  • value unknown
  • value intentionally absent
  • empty but insignificant
  • omitted only to reduce payload size

If consumers distinguish between null, an empty string and a missing field, NON_EMPTY may be the wrong choice. Pick the rule based on API semantics, not only on output size.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms