Make Selenium Easy

REST Assured Tutorial 37 – @JsonInclude Annotation – Ignore Null & Empty Values In Payload

As a part of End to End REST Assured Tutorial, in this post, We will learn about an important annotation @JsonInclude of Jackson library which helps in eliminating default, null, empty, etc values. For this post, we will focus on excluding null and empty values in a payload.

Maven Dependency

Since we are going to use an annotation provided by Jackson API, we need to add the Jackson library in our project. I am using maven as a build tool here.

Always try to use the latest dependency from the Central Maven Repository. I will use the below dependency at the time of writing this post.

We have already learned about @JsonInclude annotation and different values of Include enum in the previous post.

Usage of NON_NULL

In Java, a default value is provided to class variables. A chart is below:-

Data TypeDefault Value (for fields)
String (or any object)null

The most dangerous default value is NULL. If we do not put checks or validation of null values we may end up with NullPointerException. Sometimes ignoring all default values is not good and we just want to ignore NULL values. In that case, we can use NON_NULL of Include enum as shown below:-

Converting Java Object to JSON Object


We have not set values for fields ‘lastName’, ‘gender’,’age’ and ‘salary’. Since default values of fields ‘lastName’ and ‘gender’ are NULL which will be ignored during serialization but default values of fields ‘age’ and ‘salary’ are zero, so they will be included in JSON which you can see in above output.

Usage of NON_EMPTY

The scope of NON_EMPTY is greater than NON_NULL or we can say NON_EMPTY includes NON_NULL in it with others. As per the official document:-


Properties of a class will not be included during serialization if the value of the property is NULL or Empty. NULL we have already seen. It is for String and reference variables. EMPTY is a little tricky. The definition of emptiness depends on the data type of field.

  1. A NULL value is also considered as EMPTY.
  2. For Collections and Map, isEmpty() method is called to check emptiness i.e. without any content.
  3. For an array and string, the length is checked for emptiness.

A POJO with different data types:-

Let’s not set any value for any field of POJO class:-


We can see how fields are initialized with default values. Obviously we can ignore NULL values using NON_NULL which we have seen above. Let’s add NON_NULL at the class level and create an object as below:-


You can see NON_NULL did not serve the purpose here as we have empty values not null.

Let’s add NON_EMPTY at class level as:-

Let’s rerun both @Test methods again:-

Output of first @Test Method:-

Output of second @Test Method:-

I have use annotation at class level but you can use it at property level as well.

Complete Program

Output (When NON_EMPTY added at class level)

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

REST Assured Tutorial 37 – @JsonInclude Annotation – Ignore Null & Empty Values In Payload

by Amod Mahajan time to read: 6 min
%d bloggers like this: