Question
How can Jackson be configured to omit a field from JSON serialization when its value is null, while still serializing the field when it has a non-null value?
For example, given this Java class:
public class SomeClass {
private String someValue;
}
Which Jackson annotation or configuration causes someValue to be skipped when it is null but included otherwise?
Short Answer
You will learn how Jackson decides whether a Java property appears in JSON and how to omit properties whose values are null. You will see the field-level @JsonInclude(JsonInclude.Include.NON_NULL) annotation, class-wide and global alternatives, and common configuration mistakes.
Concept
Jackson serializes Java objects into JSON. By default, a property with a null value is usually written explicitly:
{
"someValue": null
}
Sometimes an API should instead omit that property entirely:
{}
Use @JsonInclude(JsonInclude.Include.NON_NULL) to tell Jackson: include this property only when its value is not null.
This matters because an omitted JSON property and a property containing null can have different meanings:
- Missing property: the sender did not provide a value.
- Property set to
null: the sender explicitly provided no value.
Whether that distinction is appropriate depends on your API contract. The annotation affects serialization—turning Java objects into JSON. It does not by itself change how JSON is deserialized into Java objects.
Mental Model
Think of each Java field as an item that may be packed into a JSON box.
- Normal Jackson behavior puts every item in the box, even an empty one labeled
null. Include.NON_NULLis a packing rule: “Only pack this item if it contains something.”
If someValue is "hello", Jackson packs it. If it is null, Jackson leaves it out.
Syntax and Examples
Import JsonInclude and apply it to the field:
import com.fasterxml.jackson.annotation.JsonInclude;
public class SomeClass {
@JsonInclude(JsonInclude.Include.NON_NULL)
private String someValue;
public SomeClass(String someValue) {
this.someValue = someValue;
}
public String getSomeValue() {
return someValue;
}
}
Serializing an instance with a value:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(new SomeClass("available"));
System.out.println(json);
Output:
{"someValue":"available"}
Serializing an instance whose field is :
Step by Step Execution
Consider this complete example:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
SomeClass withValue = new SomeClass("active");
SomeClass withoutValue = new SomeClass(null);
System.out.println(mapper.writeValueAsString(withValue));
System.out.println(mapper.writeValueAsString(withoutValue));
}
static class SomeClass {
@JsonInclude(JsonInclude.Include.NON_NULL)
private final String someValue;
SomeClass(String someValue) {
this.someValue = someValue;
}
public String getSomeValue() {
someValue;
}
}
}
Real World Use Cases
- REST API responses: Do not send optional profile fields such as
middleNameoravatarUrlwhen no value exists. - Database-backed services: Database columns that are
NULLcan be omitted from response JSON when that matches the API contract. - Partial data integrations: An external provider may not supply all details; your response can include only known values.
- Event payloads: Keep messages smaller by leaving out unused optional metadata.
- Configuration endpoints: Return only settings that have been explicitly configured.
Real Codebase Usage
In projects, developers choose the scope of null exclusion based on the API design.
One optional property
Annotate the individual field or getter:
@JsonInclude(JsonInclude.Include.NON_NULL)
private String avatarUrl;
This is best when most properties should retain their normal behavior but one property is optional.
All properties in one DTO
Annotate the class:
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserResponse {
private String id;
private String displayName;
private String avatarUrl;
}
This is common for response DTOs, where many fields are optional.
All serialized objects
Configure the shared ObjectMapper:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Common Mistakes
Using the wrong annotation
@JsonIgnore always excludes a property. It does not include it again when a value is present.
@JsonIgnore
private String someValue;
With @JsonIgnore, someValue is never serialized, including when it contains "hello".
Use this instead:
@JsonInclude(JsonInclude.Include.NON_NULL)
private String someValue;
Expecting NON_NULL to remove empty strings
An empty string is not null:
new SomeClass("");
With NON_NULL, Jackson produces:
{"someValue":""}
If you also need to omit empty strings, normalize input before serialization or use a custom serializer when appropriate.
Comparisons
| Option | What it does | When to use it |
|---|---|---|
Include.ALWAYS | Includes properties, including null values | You need explicit JSON null values |
Include.NON_NULL | Omits only properties whose values are null | Optional fields should disappear when absent |
Include.NON_EMPTY | Omits null, empty strings, empty collections, and empty arrays | Empty values are not meaningful in the JSON contract |
Include.NON_DEFAULT | Omits values equal to a property's default value | Compact output where defaults are understood by consumers |
@JsonIgnore |
Cheat Sheet
import com.fasterxml.jackson.annotation.JsonInclude;
Omit one null field
@JsonInclude(JsonInclude.Include.NON_NULL)
private String someValue;
Omit null properties for a class
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse {
// fields
}
Omit null properties globally
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
Spring Boot default
spring.jackson.default-property-inclusion=non_null
Key rules
NON_NULLomits onlynullvalues.""and[]are notnull.- Use
NON_EMPTYto omit empty strings and empty collections too.
FAQ
How do I ignore null values in Jackson?
Annotate the field, getter, or class with @JsonInclude(JsonInclude.Include.NON_NULL).
Does @JsonInclude(NON_NULL) hide a field with a real value?
No. Jackson serializes the property normally when its value is non-null.
What is the difference between NON_NULL and NON_EMPTY?
NON_NULL excludes only null. NON_EMPTY also excludes empty strings, empty collections, and empty arrays.
Can I configure Jackson to ignore nulls for every class?
Yes. Configure the shared ObjectMapper with setSerializationInclusion(JsonInclude.Include.NON_NULL), or set Spring Boot's spring.jackson.default-property-inclusion=non_null property.
Does this annotation affect JSON deserialization?
Its purpose is controlling inclusion during serialization. It does not prevent incoming JSON null values from being deserialized into a Java property.
Should an API omit nulls or return null values?
Follow the API contract. Omission is useful for optional data, while explicit null can be useful when clients need to distinguish “known to be empty” from “not provided.”
Mini Project
Description
Build a small Java API-style response model for a customer profile. Optional contact details should be omitted from JSON when they have no value, while required information remains present.
Goal
Serialize customer profiles to clean JSON without null-valued optional fields.
Requirements
Requirement 1
Keep learning
Related questions
Add External JAR Files to an IntelliJ IDEA Java Project
Learn how to add external JAR dependencies to an IntelliJ IDEA Java project using module libraries, and when to use Maven or Gradle instead.
Avoiding Java Code in JSP with JSP 2: EL and JSTL Explained
Learn how to avoid Java scriptlets in JSP 2 using Expression Language and JSTL, with examples, best practices, and common mistakes.
Call a Method After a Delay in Android Java
Learn how to run Java code after a delay in Android using Handler.postDelayed, manage the main thread, and cancel callbacks safely.