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>
  • Or, if you are not using Maven, you can download the Jackson library from here.
  • Define your POJO class with the @JsonIgnore annotation.
  • Use the ObjectMapper class to serialize the object to JSON.
  • Use the ObjectMapper class to deserialize the JSON back to the object. And again serialize it to JSON.
  • 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.

    Aishwarya Naglot
    Aishwarya Naglot

    Writing clean code… when the bugs aren’t looking.

    Updated on: 2025-09-01T13:31:48+05:30

    14K+ Views

    Kickstart Your Career

    Get certified by completing the course

    Get Started
    Advertisements