
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 can we ignore the fields during JSON serialization in Java?
If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.
In this article, we will learn how to ignore the fields during JSON serialization in Java using the Jackson library.
Steps to ignore the fields during JSON serialization in Java:
- In order to use Jackson, you will need to add it to your project. If you use Maven, add the following dependency to your pom.xml file:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version> </dependency>
Example
In the below example, we will create a class Person with fields for name and age. We will then serialize this POJO into JSON using Jackson's ObjectMapper class.
Following is the code to ignore the fields during JSON serialization in Java:
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; @JsonIgnore public class Person { private String name; private int age; @JsonIgnore private String address; public Person(String name, int age, String address) { this.name = name; this.age = age; this.address = address; } public String getName() { return name; } public int getAge() { return age; } public String getAddress() { return address; } public static void main(String[] args) throws IOException { Person person = new Person("Ansh", 23, "123 Main St"); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString(person); System.out.println(jsonString); } }
Output
Following is the output of the above code:
{"name":"Ansh","age":23}
As we can see, the address field is not included in the JSON output.
Ignoring multiple fields during JSON serialization
We can also use the @JsonIgnoreProperties annotation to ignore multiple fields at once. This annotation can be used at the class level.
In the below example, we will create a class Person with fields for name and age. We will then serialize this POJO into JSON using Jackson's ObjectMapper class.
Example
Following is the code to ignore multiple fields during JSON serialization in Java:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; @JsonIgnoreProperties({"address", "phone"}) public class Person { private String name; private int age; private String address; private String phone; public Person(String name, int age, String address, String phone) { this.name = name; this.age = age; this.address = address; this.phone = phone; } public String getName() { return name; } public int getAge() { return age; } public String getAddress() { return address; } public String getPhone() { return phone; } public static void main(String[] args) throws IOException { Person person = new Person("Ansh", 23, "123 Main St", "123-456-7890"); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = objectMapper.writeValueAsString(person); System.out.println(jsonString); } }
Output
Following is the output of the above code:
{"name":"Ansh","age":23}
As we can see, the address and phone fields are not included in the JSON output.