Jackson @JsonIgnore, @JsonIgnoreProperties and @JsonIgnoreType

In this tutorial, I show you how to ignore certain fields when serializing an object to JSON using Jackson @JsonIgnore, @JsonIgnoreProperties and @JsonIgnoreType annotations with an example. These annotations are used to ignore logical properties in JSON serialization and deserialization.
  • @JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setter, getter or field.
  • @JsonIgnoreProperties ignores the specified logical properties in JSON serialization and deserialization. It is annotated at the class level.
  • @JsonIgnoreType is annotated at the class level and it ignores the complete class.
This is very useful when the Jackson defaults aren’t enough and we need to control exactly what gets serialized to JSON.  Let's demonstrates the usage of these annotations with examples.

Table of contents

  1. Maven Dependencies 
  2. Ignore Fields at the Class Level using @JsonIgnoreProperties
  3. Ignore Field at the Field Level using @JsonIgnore
  4. Ignore all Fields by Type using @JsonIgnoreType

1. Maven Dependencies

Let’s first add the following dependencies to the pom.xml:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
  • jackson-annotations-2.9.8.jar
  • jackson-core-2.9.8.jar
  • jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.

2. Ignore Fields at the Class Level using @JsonIgnoreProperties

We can ignore specific fields at the class level, using the @JsonIgnoreProperties annotation and specifying the fields by name:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {
    "id",
    "firstName"
})
public class CustomerDTO {
    private final String id;
    private final String firstName;
    private final String lastName;

    public CustomerDTO(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IgnoreFieldTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");

        String dtoAsString = mapper.writeValueAsString(dtoObject);

        System.out.println(dtoAsString);
    }
}
Output:
{"lastName":"Stark"}
Note that we have ignored two fields "id" and "firstName" of CustomerDTO and printed only "lastName".

3. Ignore Field at the Field Level using @JsonIgnore

We can also ignore a field directly via the @JsonIgnore annotation directly on the field:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class CustomerDTO {

    @JsonIgnore
    private final String id;

    @JsonIgnore
    private final String firstName;
    private final String lastName;

    public CustomerDTO(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getId() {
        return id;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class IgnoreFieldTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();

        CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");

        String dtoAsString = mapper.writeValueAsString(dtoObject);

        System.out.println(dtoAsString);
    }
}
Output:
{"lastName":"Stark"}
Note that we have ignored two fields "id" and "firstName" of CustomerDTO using @JsonIgnore annotation and printed only "lastName".

3. Ignore all Fields by Type using @JsonIgnoreType

We can also ignore all fields of a specified type, using the @JsonIgnoreType annotation. If we control the type, then we can annotate the class directly:
@JsonIgnoreType
public static class Name {
    public String firstName;
    public String lastName;
    public Name(String firstName, String lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
Here is the complete code:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.annotation.JsonIgnoreType;

public class UserDTO {

    public int id;
    public Name name;


    public UserDTO(int id, Name name) {
        super();
        this.id = id;
        this.name = name;
    }

    @JsonIgnoreType
    public static class Name {
        public String firstName;
        public String lastName;
        public Name(String firstName, String lastName) {
            super();
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonIgnoreTypeTest {

    public static void main(String[] args) throws JsonProcessingException {
        UserDTO.Name name = new UserDTO.Name("John", "Doe");
        UserDTO user = new UserDTO(1, name);

        String result = new ObjectMapper()
            .writeValueAsString(user);

        System.out.println(result);
    }
}
Output:
{"id":1}
Note that the Name class field is ignored by using @JsonIgnoreType annotation.

Related Articles

GitHub Repository

The source code of this article available on my GitHub repository at https://github.com/RameshMF/jackson-json-tutorial

Comments