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.
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.
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.
With this rule:
- '
nullfields 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.
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.
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
With NON_EMPTY, the output contains only the non-empty data.
Common Pitfalls
- Using
NON_EMPTYwhen clients actually need to distinguish between missing and empty values. - Assuming
NON_NULLwill 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_NULLomits onlynullfields.' - '
NON_EMPTYomitsnullplus 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
- Jackson serializes a ZonedDateTime wrongly in Spring Boot
- Jackson Vs. Gson
- Jackson with JSON Unrecognized field, not marked as ignorable
- Java-R integration?
- Java8 Why is it forbidden to define a default method for a method from java.lang.Object
- Java - Convert integer to string
- Java - escape string to prevent SQL injection
- Java - get the current class name?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.