Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Introduction

When we work with Java objects and need to convert them into JSON format, it’s crucial to handle null values appropriately. Omitting null values from the JSON output might not align with our data requirements, especially when data integrity is paramount.

In this tutorial, we’ll delve into effective methods for including null values during JSON serialization in Java.

2. Use Case Scenario: Customer Management System

Let’s consider that we’re developing a customer management system where each customer is a Java object with attributes like name, email, and age. Moreover, some customers may lack email addresses.

When serializing customer data into JSON for storage or transmission, including null values for missing email addresses is crucial to maintain data consistency.

For the sake of completeness, let’s define the Customer class used in our examples:

public class Customer {
    private final String name;
    private final String address;
    private final int age;

    public Customer(String name, String address, int age) { = name;
        this.address = address;
        this.age = age;

    // ...

Here, we define the Customer class which includes fields for name, address, and age, initialized through a constructor method. The toString() method also provides a string representation of the object in a JSON-like format for easy debugging.

Please note that including @JsonProperty annotations ensures that all relevant fields are accurately serialized into the JSON output, including null values when applicable.

3. Using Jackson Library

Jackson, a renowned Java library for JSON processing, typically excludes null values from JSON output by default. However, we can leverage Jackson’s annotations to include null values explicitly:

String expectedJson = "{\"name\":\"John\",\"address\":null,\"age\":25}";
Customer obj = new Customer("John", null, 25);

public void givenObjectWithNullField_whenJacksonUsed_thenIncludesNullValue() throws JsonProcessingException {
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(obj);
    assertEquals(expectedJson, json);

In this method, we begin by establishing the expectedJson string. Subsequently, we instantiate a Customer object with values John, null, and 25.

Moving forward, we configure an ObjectMapper instance to consistently include null values during serialization by invoking the setSerializationInclusion() method with the parameter JsonInclude.Include.ALWAYS. This meticulous setup ensures that even if the address is null, it’s accurately represented in the resulting JSON output.

Lastly, we use the assertEquals() method to verify that the json string matches the expectedJson.

4. Using Gson Library

Gson is an alternative Java library that can be used for JSON serialization and deserialization. It offers similar options to include null values explicitly. Let’s see an example:

public void givenObjectWithNullField_whenGsonUsed_thenIncludesNullValue() {
    Gson gson = new GsonBuilder().serializeNulls().create();
    String json = gson.toJson(obj);
    assertEquals(expectedJson, json);

Here, we employ the GsonBuilder to set up a Gson instance using the serializeNulls() method. This configuration ensures that null values are incorporated during serialization, ensuring precise representation in the resulting JSON output, even if the address field is null.

5. Conclusion

In conclusion, it’s essential to handle null values appropriately when working with Java objects and converting them into JSON format.

In this tutorial, we learned how to do it during JSON serialization.

As always, the source code for the examples is available over on GitHub.

Course – LS (cat=JSON/Jackson)

Get started with Spring and Spring Boot, through the Learn Spring course:

res – Jackson (eBook) (cat=Jackson)
Notify of
Inline Feedbacks
View all comments