Main Tutorials

How to ignore a field with Jackson

In Jackson, we can use @JsonIgnore to ignore a single field, @JsonIgnoreProperties to ignore multiple fields and @JsonIgnoreType to ignore a specified type during JSON serialization and deserialization.

Table of contents:

P.S Tested with Jackson 2.17.0

1. Setup Jackson

Puts jackson-databind at pom.xml.

pom.xml

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.17.0</version>
  </dependency>

2. Jackson @JsonIgnore – Ignores a field

The annotation @JsonIgnore is used directly on the field or getter/setter to ignore it during JSON serialization and deserialization.

User.java

package com.mkyong.json.model;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class User {

    private String name;

    @JsonIgnore
    private int age;

    // getters, setters, constructors and etc
}

Testing ignoring fields with Jackson.

IgnoreFieldExample.java

package com.mkyong.json.jackson.tips;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.User;

public class IgnoreFieldExample {

    public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();

        User user = new User();
        user.setName("mkyong");
        user.setAge(42);
        
        try {

            String jsonOutput = mapper.writeValueAsString(user);
            System.out.println(jsonOutput);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}

output


{"name":"mkyong"}

3. Jackson @JsonIgnoreProperties – Ignores multiple fields

The annotation @JsonIgnoreProperties is used at the class level to ignore multiple fields by name.

User.java

package com.mkyong.json.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = {"name", "age"})
public class User {

    private String name;

    private int age;

    // getters, setters, constructors and etc
}

Output


{}

4. Jackson @JsonIgnoreType – Ignores a specified type

The annotation @JsonIgnoreType is used on a class definition to automatically ignore all fields of this class.

SecretData

package com.mkyong.json.model;

import com.fasterxml.jackson.annotation.JsonIgnoreType;

@JsonIgnoreType
public class SecretData {

    private String password;

    private String type;

    // getters, setters, constructors and etc
}
User.java

package com.mkyong.json.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

public class User {

    private String name;

    private int age;

    // this class with ignore automatically
    private SecretData secret;

    // getters, setters, constructors and etc
}

Testing ignoring a specified type with Jackson.

IgnoreFieldExample.java

package com.mkyong.json.jackson.tips;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mkyong.json.model.SecretData;
import com.mkyong.json.model.User;

public class IgnoreFieldExample {

    public static void main(String[] args) {

        ObjectMapper mapper = new ObjectMapper();

        User user = new User();
        user.setName("mkyong");
        user.setAge(42);
        user.setSecret(new SecretData("123", "sha256"));

        try {

            String jsonOutput = mapper.writeValueAsString(user);
            System.out.println(jsonOutput);

        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}

output


{"name":"mkyong","age":42}

5. Download Source Code

6. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments