Skip to content

Jackson Ignore null fields

Jackson Ignore null fields

1. Overview

In this article, we will learn to ignore null fields during Serialization in Jackson. To learn more about Jackson, refer to these articles. Here, Serialization is converting a Java object to JSON.

2. Jackson Ignore null fields

If your Jackson library version is 2.0 or above, then check the below section. Otherwise, navigate to this section directly that provides the solutions for the below 2.0 versions.

2.1. Jackson 2.0 and above

Let’s see the various possibilities of ignoring the null fields in Jackson 2.0 and the above versions.

Ignoring null fields globally using ObjectMapper

You can configure the ObjectMapper to ignore serializing the properties or fields with null values. After you configure, any serialization that happens using this ObjectMapper would ignore the null fields.

mapper.setSerializationInclusion(Include.NON_NULL);

Consider we have a Student class that has id and studentName properties. In the below code, we have a student object with id 101 and studentName null.

Since we set the mapper to include only non-null fields during serialization, the studentName will not appear in the JSON. The JSON would be {"id":101}.

@SpringBootApplication
public class JacksonFieldNameApplication implements CommandLineRunner {
	public static void main(String[] args) {
		SpringApplication.run(JacksonFieldNameApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(Include.NON_NULL);
		Student student = new Student(101, null);
		System.out.println(mapper.writeValueAsString(student));   
                
	}
}

public class Student {
	public Student(long id, String name) {
		this.id = id;
		this.studentName = name;
	}
	private long id;
	private String studentName;
}

Ignore null fields globally using properties file

You can also ignore the null fields by defining the spring.jackson.default-property-inclusion property in the properties file. It can accept any values defined in the JsonInclude.Include enumeration (always, non_null, non_absent, non_default, non_empty).

Application.properties file

spring.jackson.default-property-inclusion=non_null

application.yml file

spring:
  jackson:
    default-property-inclusion: non_null

Ignore null fields globally using Jackson2ObjectMapperBuilder

Jackson2ObjectMapperBuilder is a builder to create instances of ObjectMapper and performs auto-configuration. You can create Jackson2ObjectMapperBuilder and configure it to ignore the null fields during serialization.

import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.annotation.JsonInclude;

@Configuration
public class JacksonConfiguration {
    @Bean
    public Jackson2ObjectMapperBuilder objectMapperBuilder() {
    	Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        builder.failOnUnknownProperties(false);
        return builder;
    }
}

You must use the Jackson2ObjectMapperBuilder to create your ObjectMapper instance. In the below code, we are injecting the Jackson2ObjectMapperBuilder bean. Then, we are creating an ObjectMapper instance using it.

This ObjectMapper ignores the null fields during serialization.

@SpringBootApplication
public class JacksonFieldNameApplication implements CommandLineRunner {
	public static void main(String[] args) {
		SpringApplication.run(JacksonFieldNameApplication.class, args);
	}

	@Autowired
	private Jackson2ObjectMapperBuilder objectMapperBuilder;

	@Override
	public void run(String... args) throws Exception {
		
		ObjectMapper mapper = objectMapperBuilder.build();
		Student student = new Student(101, null);
		System.out.println(mapper.writeValueAsString(student));
                    
	}
}

Ignore specific fields if null using @JsonInclude

You can apply the @JsonInclude directly to the Java property or field. For example, you want to control the exclusion of null values on a per-field basis and you are already aware of the fields that can be null in advance.

private long id;

@JsonInclude(Include.NON_NULL)
private String studentName;

You can also apply this to the getter or setter method of the field.

@JsonInclude(Include.NON_NULL)
public String getStudentName() {
	return studentName;
}
(or)
@JsonInclude(Include.NON_NULL)
public void setStudentName(String studentName) {
	this.studentName = studentName;
}

2.2. Jackson above 1.9.11 and below 2.0

For earlier versions of Jackson, you need to use the @JsonSerialize annotation. @JsonValue annotation is available only from 2.0 and above.

As of Jackson 2.0, Jackson replaces this annotation with com.fasterxml.jackson.annotation.JsonInclude.

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 

Also to set globally, you can configure the ObjectMapper:

mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);

3. Conclusion

To sum up, we have learned the various solutions available to ignore null fields during serialization in Jackson. You can choose any solution that best fits your application.

Leave a Reply

Your email address will not be published. Required fields are marked *