

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Syntax
@Target(value={ANNOTATION_TYPE,METHOD,CONSTRUCTOR,FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnore
Example
import java.io.*; 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"); System.out.println(customer); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(customer); System.out.println("JSON: " + jsonString); System.out.println("---------"); jsonString = "{\"id\":\"120\",\"name\":\"Devaraj\", \"address\":\"Banglore\"}"; System.out.println("JSON: " + jsonString); customer = mapper.readValue(jsonString, Customer.class); System.out.println(customer); } } // Customer class 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 String getName() { return name; } public String getAddress() { return address; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; } }
Output
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]
- Related Questions & Answers
- How to convert Java object to JSON using Jackson library?
- How to convert a JSON to Java Object using the Jackson library in Java?
- Convert CSV to JSON using the Jackson library in Java?
- Pretty print JSON using Jackson library in Java?
- How to ignore the null and empty fields using the Jackson library in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- Convert JSON to/from Map using Jackson library in Java?
- How to ignore the multiple properties of a JSON object in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?
- How to ignore a class during the serialization using Jackson in Java?
- How to convert a JSON object to an enum using Jackson in Java?
- How can we change a field name in JSON using Jackson in Java?
- Convert JSON object to Java object using Gson library in Java?
- Convert Java object to JSON using the Gson library in Java?
- How to convert Java object to JSON using GSON library?
Advertisements