Ignore Null Fields with Jackson

1. Overview

This quick tutorial is going to cover how to set up Jackson to ignore null fields when serializing a java class.

If we want to dig deeper and learn other cool things to do with the Jackson 2, we can head on over to the main Jackson tutorial.

2. Ignore Null Fields on the Class

Jackson allow us to control this behavior at either the class level:

@JsonInclude(Include.NON_NULL)
public class MyDto { ... }

Or with more granularity at the field level:

public class MyDto {

    @JsonInclude(Include.NON_NULL)
    private String stringValue;

    private int intValue;

    // standard getters and setters
}

Now we should be able to test that null values are indeed not part of the final JSON output:

@Test
public void givenNullsIgnoredOnClass_whenWritingObjectWithNullField_thenIgnored()
  throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    MyDto dtoObject = new MyDto();

    String dtoAsString = mapper.writeValueAsString(dtoObject);

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, not(containsString("stringValue")));
}

3. Ignore Null Fields Globally

Jackson also allows us to configure this behavior globally on the ObjectMapper:

mapper.setSerializationInclusion(Include.NON_NULL);

Now any null field in any class serialized through this mapper is going to be ignored:

@Test
public void givenNullsIgnoredGlobally_whenWritingObjectWithNullField_thenIgnored() 
  throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_NULL);
    MyDto dtoObject = new MyDto();

    String dtoAsString = mapper.writeValueAsString(dtoObject);

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
    assertThat(dtoAsString, not(containsString("stringValue")));
}

4. Conclusion

Ignoring null fields is such a common Jackson configuration because it’s often the case that we need to have better control over the JSON output. This article demonstrates how to do that for classes. There are, however, more advanced use cases, such as ignoring null values when serializing a Map.

The implementation of all of these examples and code snippets can be found in the Github project.

Related posts:

Java Program to Implement Pollard Rho Algorithm
Connect through a Proxy
Java Program to Implement CopyOnWriteArraySet API
Java Program to Describe the Representation of Graph using Incidence List
Java Program to Implement Rolling Hash
Debug a JavaMail Program
ArrayList trong java
Spring MVC + Thymeleaf 3.0: New Features
Using a Mutex Object in Java
Logging in Spring Boot
Java Program to Implement Sorted Doubly Linked List
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Implement Miller Rabin Primality Test Algorithm
Java Program to Implement Euclid GCD Algorithm
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Hướng dẫn Java Design Pattern – Dependency Injection
Java Program to Implement Expression Tree
Java Program to Implement Control Table
Java Program to Perform Searching in a 2-Dimension K-D Tree
Initialize a HashMap in Java
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Interpolation Search Algorithm
Java Program to Implement Floyd Cycle Algorithm
Period and Duration in Java
Using JWT with Spring Security OAuth
Java Program to Evaluate an Expression using Stacks
Intro to the Jackson ObjectMapper
Encode/Decode to/from Base64
Spring’s RequestBody and ResponseBody Annotations
Spring Security 5 for Reactive Applications