Jackson
serialization
ignore-empty-values
JSON
Java

Jackson serialization ignore empty values or null

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Jackson can omit fields during JSON serialization, but the exact behavior depends on which inclusion rule you choose. NON_NULL ignores only null values. NON_EMPTY also ignores empty strings, empty collections, and empty arrays. The right choice depends on whether an empty value is semantically meaningful in your API or just noise.

Ignore Only null Values

If you want to omit fields only when they are null, use Include.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, Jackson leaves it out of the JSON. If email is an empty string, it is still serialized.

Ignore Empty Values Too

If you also want to omit empty strings, empty collections, and similar values, use 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 this rule:

  • 'null fields are omitted'
  • '"" is omitted'
  • empty lists are omitted
  • empty arrays are omitted

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

Configure It Globally on the ObjectMapper

Instead of annotating every class, you can configure the ObjectMapper 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);

This makes the rule apply to everything serialized by that mapper.

Global configuration is useful when the API has one consistent serialization policy.

Override at the Field Level When Needed

Sometimes most of the object should use one rule, but one field needs different behavior. Jackson annotations can be applied at the field level too.

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 is helpful when an empty string or empty collection still needs to be preserved on some fields but not others.

Choose the Rule Once Per API Boundary

It is usually better to decide inclusion rules at the DTO or mapper boundary rather than scattered deep inside domain objects. That keeps serialization policy visible and prevents one team's payload cleanup rule from unexpectedly changing another endpoint's meaning.

Be Clear About API Semantics

The technical part is easy. The design part is harder.

For example, omitting a field can mean:

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

Those are not the same business meaning. If API consumers distinguish between null, empty string, and missing field, aggressive omission may be the wrong choice.

A Small Example

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}

With NON_EMPTY, the output contains only the non-empty data.

Common Pitfalls

  • Using NON_EMPTY when clients actually need to distinguish between missing and empty values.
  • Assuming NON_NULL will also suppress empty strings or empty collections when it will not.
  • Applying a global inclusion rule without checking whether all DTOs share the same API semantics.
  • Forgetting that field-level annotations can override a broader class or mapper strategy.
  • Treating payload size as the only concern when serialization rules also shape API meaning.

Summary

  • 'NON_NULL omits only null fields.'
  • 'NON_EMPTY omits null plus empty strings, collections, and arrays.'
  • You can apply inclusion rules at the class level, field level, or mapper level.
  • Pick the rule based on API semantics, not only on output size.
  • Be careful when missing, null, and empty have different business meanings.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.