Jackson JSON – Ignore Properties with @JsonIgnore and @JsonIgnoreProperties

jackson-json-ignore-properties-jsonignore-jsonignoreproperties-feature-image

In this tutorial, we’re gonna look at ways to ignore Properties of a Class using @JsonIgnore and @JsonIgnoreProperties.

I. Maven Dependency

	
		com.fasterxml.jackson.core
		jackson-databind
		2.9.4
	

II. @JsonIgnore

Use @JsonIgnore on fields or getters or setters to ignore individual properties:
Customer class:

package com.javasampleapproach.jackson.JsonIgnore;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Customer {
	private String id;
	private String name;
	@JsonIgnore
	private String address;

	public Customer() {
	}

	public Customer(String id, String name, String address) {
		this.id = id;
		this.name = name;
		this.address = address;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
	}
}

App class for testing:

package com.javasampleapproach.jackson.JsonIgnore;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class App {

	public static void main(String[] args) throws IOException {
		Customer customer = new Customer("1", "Jack Smith", "Massachusetts");
		System.out.println("print: " + customer);

		ObjectMapper om = new ObjectMapper();
		String jsonString = om.writeValueAsString(customer);
		System.out.println("print json: " + jsonString);

		System.out.println("---------");
		
		jsonString = "{\"id\":\"2\",\"name\":\"Adam Johnson\", \"address\":\"Texas\"}";
		System.out.println("input json: " + jsonString);

		customer = om.readValue(jsonString, Customer.class);
		System.out.println("print: " + customer);
	}
}

>> Result:

print: Customer [id=1, name=Jack Smith, address=Massachusetts]
print json: {"id":"1","name":"Jack Smith"}
---------
input json: {"id":"2","name":"Adam Johnson", "address":"Texas"}
print: Customer [id=2, name=Adam Johnson, address=null]
III. @JsonIgnoreProperties

Use @JsonIgnoreProperties to ignore multiple properties with just one declaration:
Customer class:

package com.javasampleapproach.jackson.JsonIgnoreProperties;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties({ "id", "address" })
public class Customer {
	private String id;
	private String name;
	private String address;

	public Customer() {
	}

	public Customer(String id, String name, String address) {
		this.id = id;
		this.name = name;
		this.address = address;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
	}
}

App class for testing:

package com.javasampleapproach.jackson.JsonIgnoreProperties;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class App {

	public static void main(String[] args) throws IOException {
		Customer customer = new Customer("1", "Jack Smith", "Massachusetts");
		System.out.println("print: " + customer);

		ObjectMapper om = new ObjectMapper();
		String jsonString = om.writeValueAsString(customer);
		System.out.println("print json: " + jsonString);

		System.out.println("---------");

		jsonString = "{\"id\":\"2\",\"name\":\"Adam Johnson\", \"address\":\"Texas\"}";
		System.out.println("input json: " + jsonString);

		customer = om.readValue(jsonString, Customer.class);
		System.out.println("print: " + customer);
	}
}

>> Result:

print: Customer [id=1, name=Jack Smith, address=Massachusetts]
print json: {"name":"Jack Smith"}
---------
input json: {"id":"2","name":"Adam Johnson", "address":"Texas"}
print: Customer [id=null, name=Adam Johnson, address=null]

10 thoughts on “Jackson JSON – Ignore Properties with @JsonIgnore and @JsonIgnoreProperties”

  1. I’m curious to find out what blog platform you’re using? I’m experiencing some minor security issues with my latest site and I’d like to find something more safe. Do you have any suggestions?

  2. 197516 820610Hi my loved one! I wish to say that this post is remarkable, wonderful written and come with almost all essential infos. I would like to see more posts like this . 601716

Leave a Reply

Your email address will not be published. Required fields are marked *