How to ignore a field of JSON object using the Jackson library in Java?

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON. We can use the readValue() and writeValueAsString() methods of ObjectMapper class to read a JSON to Java Object and to write a Java object to JSON.


public @interface JsonIgnore


import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
public class JsonIgnoreTest {
   public static void main(String[] args) throws IOException {
      Customer customer = new Customer("110", "Surya Kiran", "Chennai");
      ObjectMapper mapper = new ObjectMapper();
      String jsonString = mapper.writeValueAsString(customer);
      System.out.println("JSON: " + jsonString);
      jsonString = "{\"id\":\"120\",\"name\":\"Devaraj\", \"address\":\"Banglore\"}";
      System.out.println("JSON: " + jsonString);
      customer = mapper.readValue(jsonString, Customer.class);
// Customer class
class Customer {
   private String id;
   private String name;
   private String address;
   public Customer() {
   public Customer(String id, String name, String address) { = id; = name;
      this.address = address;
   public String getId() {
      return id;
   public String getName() {
      return name;
   public String getAddress() {
      return address;
   public String toString() {
      return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";


Customer [id=110, name=Surya Kiran, address=Chennai]
JSON: {"id":"110","name":"Surya Kiran"}
JSON: {"id":"120","name":"Devaraj", "address":"Banglore"}
Customer [id=120, name=Devaraj, address=null]

Updated on: 06-Jul-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started