Explain Codes LogoExplain Codes Logo

Jackson serialization: ignore empty values (or null)

java
json-serialization
jackson-annotations
null-values
Alex KataevbyAlex Kataev·Feb 25, 2025
TLDR

Use Jackson's @JsonInclude(JsonInclude.Include.NON_EMPTY) annotations on class definitions when you want to skip any null or empty fields during serialization:

@JsonInclude(JsonInclude.Include.NON_EMPTY) public class MyClass { private String prop; // no prop, no problem // ... }

Or tweak ObjectMapper settings for a global application:

ObjectMapper mapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

Both methods will exclude null or inherently empty fields like "" or empty collections from your JSON output.

Handling individual fields

You can use @JsonInclude annotation on individual fields when you require control over specific properties.

public class AnotherClass { @JsonInclude(JsonInclude.Include.NON_NULL) private String importantField; // Nulls need not apply 😉 private String anotherField; // ... }

This will leave anotherField unaffected while importantField gets excluded when it's null.

Complications with nested POJO classes

With POJOs nested within each other, remember to annotate these POJOs correctly:

@JsonInclude(JsonInclude.Include.NON_EMPTY) public class ParentClass { private String name; private ChildClass child; // Nested POJO should also carry the annotation @JsonInclude(JsonInclude.Include.NON_EMPTY) public static class ChildClass { private String childName; // even the little ones are included // ... } // ... }

Jackson version compatibility matter

Ensure that your Jackson version is 2.1.4 or above to enable the @JsonInclude annotation to work correctly. While upgrading, be mindful that Jackson's API changes over versions may impact serialization behavior.

Be explicit with examples

Clear examples of expected input and output in your questions or documentation always help in understanding the serialization process. And they make your work look neat too!

Dealing with older versions of Jackson

If you're using Jackson < 2.x, switch over to using @JsonSerialize instead of our favorite @JsonInclude:

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) public class LegacyClass { private String legacyProp; // a relic of another time // ... }

This annotation form is deprecated in Jackson 2.x and later, but it's clingy and refuses to leave legacy applications.

Overcoming stubborn serialization issues

If things don't go as planned (when do they ever?), check for conflicts with other annotations, verify correct import of @JsonInclude, or head directly into the frustrating realm of errors and exceptions to scope out the underlying issues.

Making cleaner JSON output with NON_NULL over NON_EMPTY

Even though NON_EMPTY skips over empty strings and collections, NON_NULL often creates cleaner and more predictable JSON. It chooses to axe only null values, not bothering with the emptiness of a data field.

Using annotations for select information in subscription APIs

When dealing with subscription-based services, annotations come in handy for controlling the information that goes into the JSON payload. Jackson ensures only vital data is included.

Choosing between global versus local settings

The flexibility of Jackson allows you to pick between field-level annotations and global ObjectMapper settings. While global settings are easy to configure, field-level annotations allow for fine-tuning of serialization behavior on a per-field basis.

Updating Jackson to resolve issues

If things go south and annotations just refuse to work, consider updating to the latest Jackson libraries. Remember, with great update comes great need for compatibility verification.