
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to ignore the null and empty fields using the Jackson library in Java?\\n
The Jackson is a library for Java and it has very powerful data binding capabilities and it also provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object.
Ignoring Null and Empty Fields Using Jackson
The Jackson library provides @JsonInclude annotation, which controls the serialization of a class as a whole or its individual fields based on their values during serialization. The @JsonInclude annotation contains below two values:
- Include.NON_NULL: Indicates that only properties with not null values will be included in JSON.
- Include.NON_EMPTY: Indicates that only properties that are not empty will be included in JSON.
Steps
Let's look at the steps in order to code the Jackson library to ignore null and empty fields.
- Import the Jackson library. If you are using Maven, add the following dependency to your pom.xml file:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.14.0</version> </dependency>
- If you do not use Maven, you can download the jar file from here.
- Next, create a class with fields for name, age, and salary.
- Use the @JsonInclude annotation at the class level, and set the value to Include.NON_NULL and Include.NON_EMPTY.
- Create a constructor and getter methods for the fields.
- Create an ObjectMapper object, and enable pretty printing using the enable(SerializationFeature.INDENT_OUTPUT) method.
- Use the writeValueAsString() method of the ObjectMapper class to convert the object to a JSON string.
- Print the JSON string.
- Use the readValue() method to convert the JSON string back to a Java object by providing the target class type as an argument (e.g., readValue(jsonString, MyClass.class)).
Example
Following is the code to ignore null and empty fields using the Jackson library:
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature; public class IgnoreNullAndEmptyFieldsExample { public static void main(String[] args) throws Exception { Person person = new Person(null, 23, null); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); String jsonString = objectMapper.writeValueAsString(person); System.out.println("JSON String: " + jsonString); Person personFromJson = objectMapper.readValue(jsonString, Person.class); System.out.println("Person Object: " + personFromJson.getName() + ", " + personFromJson.getAge() + ", " + personFromJson.getSalary()); } } @JsonInclude(JsonInclude.Include.NON_NULL) @JsonInclude(JsonInclude.Include.NON_EMPTY) class Person { private String name; private int age; private Double salary; public Person(String name, int age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public int getAge() { return age; } public Double getSalary() { return salary; } }
The output of the above code will be:
JSON String: { "age" : 23 } Person Object: null, 23, null
As you can see, the null and empty fields are ignored in the JSON string.
Advertisements