3 ways to ignore null fields while converting Java object to JSON using Jackson

Ignoring null fields or attribute is a one of the common requirement while marshaling Java object to JSON string because Jackson just prints null when a reference filed is null, which you may not want. For example, if you have a Java object which has a String field whose value is null when you convert this object to Json, you will see null in front of that. In order to better control JSON output, you can ignore null fields, and Jackson provides a couple of options to do that. You can ignore null fields at the class level by using @JsonInclude(Include.NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null.

You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if its null. You can also ignore nulls at the object mapper level, e.g. by configuring it to ignore nulls globally.

I'll show you the example of each of these three ways to ignore null fields using Jackson, but before that let's first see an example of marshaling Java object with null fields using Jackson to understand the problem better.

Btw, I expect you that you know Java and familiar with using third party libraries like Jackson in your code. If you happen to just start with Java or want to refresh your Java knowledge, I suggest you to first go through a comprehensive online Java course like The Complete Java Masterclass. It's also the most up-to-date course and covers new features from recent Java versions.




What happens when a field is null?

Suppose you have an object which has a String field to contain the author, Now if you create a book with the null author, and you want to convert that into JSON, what will happen? A NullPointerException or something else? let's see


public class JacksonTest {
 
  public static void main(String args[]) throws JsonProcessingException {
    // let's create a book with author as null
    Book cleanCode = new Book("Clean Code", null, 42);
 
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(cleanCode);
 
    System.out.println(json);
 
  }
}

This will print the following output:

{"title":"Clean Code","author":null,"price":42}

You can see that the author is printed as null, which may not be ideal for many. This is still better than a NullPointerException but you could have been even better if the author field was not included in the output altogether. And, if that's precisely what you want, then you can ignore the fields with null value using @JsonInclude annotation in Jackson.

3 ways to ignore null fields while converting Java object to JSON using Jackson


Ignoring null fields at field level in Jackson

Now, let's first see how we can ignore the fields with null values in JSON output at field level. We can do this by annotating each field with @JsonInclude(Include.NON_NULL) annotation. If a field is annotated by this, then it will be not included in the JSON output if its null.

Here is an example to confirm this:

public class Book implements Comparable<Book> {
  private String title;
 
  @JsonInclude(Include.NON_NULL)
  private String author;
 
  private int price;
 
  public Book(String title, String author, int price) {
    this.title = title;
    this.author = author;
    this.price = price;
  }
 
}

If you rerun the main method, this time it will produce a different output, I mean, without the author field as shown below:

{"title":"Clean Code","price":42}

But if you make the title null, then it will be the same issue as we have only ignored the author. So this solution makes sense to annotate all optional fields with @JsonInclude(Include.NON_NULL).

Btw, I am assuming here that you are familiar with JSON structure and JSON itself in general, If you are not, you can see this Introduction to JSON course on Udemy to understand the structure and properties of JSON.

best course to learn JSON for Programmers Udemy



Excluding null fields at class level

Jackson also allows you to ignore null fields at the class level, which means every field which has null value will be ignored. This is same as annotating all fields with @JsonInclude(Include.NON_NULL) annotation.

So if you have a requirement where all the fields are optional or may contain null then instead of annotating every single field it's better to do it once at the class level as shown below:

@JsonInclude(Include.NON_NULL)
public class Book implements Comparable<Book> {
  private String title;
  private String author;
  private int price;
 
  public Book(String title, String author, int price) {
    this.title = title;
    this.author = author;
    this.price = price;
  }
}


If you run the main class, we will again get the same JSON output as we got in the previous example, as shown below:

{"title":"Clean Code","price":42}

but if you make the title also null then you only get the price in the JSON output:

public class JacksonTest {
 
  public static void main(String args[]) throws JsonProcessingException {
 
    // let's create a book with author as null
    Book cleanCode = new Book(null, null, 42);
 
    ObjectMapper mapper = new ObjectMapper();
    String json = mapper.writeValueAsString(cleanCode);
 
    System.out.println(json);
  }
 
}
 

Output:
{"price":42}

This happens because the Book class is annotated with @JsonInclude(Include.NON_NULL) which will exclude any null field. In this case, both the title and author were ignored.

You can see Jackson is a very popular and efficient Java library to map Java object to JSON and vice-versa. If you want to learn the basics of the Jackson library and how to use them, I suggest you take a look at the Java: JSON Databinding with Jackson course on Pluralsight. One of the best course to learn Jackson API for Java developers.

best course to learn Jackson API for Java developers

Btw, you would need a Pluralsight membership to get access this course, which cost around $29 per month or $299 annually (14% discount).

If you don't have Pluralsight membership, I encourage you to get one because it gives you access of their 5000+ online courses on all the latest topics like front-end and back-end development, machine learning, etc. It also includes interactive quizzes, exercises, and latest certification material.

Alternatively, you can also sign up for their 10-day free trial, which provides 200 minutes of watch time to any course. You can use that time to access this course free.


Ignoring null fields Globally

You can also exclude null fields globally while converting Java object to JSON by configuring this behavior on the ObjectMapper itself. Which means all the classes will have this feature by itself, and any field with null values will be ignored.

You can also configure ObjectMapper to ignore null fields globally by calling the setSerializationInclusion(Include.NON_NULL) method, as shown in the following example:

public class JacksonTest {
 
  public static void main(String args[]) throws JsonProcessingException {
 
    // let's create a book with author as null
    Book cleanCode = new Book(null, null, 42);
 
    ObjectMapper mapper = new ObjectMapper();
 
    // configure ObjectMapper to exclude null fields whiel serializing
    mapper.setSerializationInclusion(Include.NON_NULL);
 
    String json = mapper.writeValueAsString(cleanCode);
 
    System.out.println(json);
  }
 
}

Output
{"price":42}

This time also you get the same output even though you don't have @JsonInclude(Include.NON_NULL) at the top of your Book class because ObjectMapper will automatically ignore any field with a null value when this option is set.

That's all about how to ignore null fields while converting Java object to JSON string using Jackson. You have 3 ways to do it, ignore fields with null value at the field level, or class level, or globally at the ObjectMapper level. The rule of thumb is that you do it at field level for greater control and start with annotating the optional field which can be null.


Further Learning
JSON with Java APIs
Introduction to JavaScript Object Notation (JSON)
JSON in Action: Build JSON-Based Applications 

Other JSON tutorials and courses you may like:
  • How to parse JSON using Gson? (tutorial)
  • 5 JSON parsing libraries Java Developers Should Know (libraries)
  • How to parse a JSON array in Java? (tutorial)
  • How to solve UnrecognizedPropertyException in Jackson? (solution)
  • How to convert JSON to HashMap in Java? (guide)
  • 10 Things Java developer should learn in 2019?  (article)
  • How to ignore unknown properties while parsing JSON in Java? (tutorial)
  • How to parse JSON with date fields in Java? (example)
  • 5 Courses to learn RESTful  API and Web services in Java? (courses)
  • 10 free courses to learn Java in-depth (resource)
Thanks for reading this article so far. If you like Jackson JSON tutorial, then please share with your friends and colleagues. If you have any questions or feedback, then please drop a note.

P. S. - If are a complete beginner about JSON (JavaScript Object Notation) I strongly suggest you go through Introduction to JSON course on Udemy to understand the structure and properties of JSON. This will help you a lot while dealing with JSON output, formating them and producing them from your own API.

No comments:

Post a Comment