
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How can we ignore the fields during JSON serialization in Java?
If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.
Syntax
public @interface JsonIgnore
Example
import java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.annotation.*; public class JsonIgnoreAnnotationTest { public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException { Employee emp = new Employee(); emp.setFirstName("Raja"); emp.setLastName("Ramesh"); emp.setEmpId(120); emp.getTechnologies().add("Java"); emp.getTechnologies().add("Scala"); emp.getTechnologies().add("Python"); ObjectMapper mapper = new ObjectMapper(); mapper.writerWithDefaultPrettyPrinter().writeValue(System.out, emp); } } // Employee class @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "firstName", "lastName", "technologies", "empId" }) class Employee { @JsonProperty("EMPLOYEE_ID") private int empId; @JsonProperty("EMPLOYEE_FIRST_NAME") private String firstName; @JsonProperty("EMPLOYEE_LAST_NAME") private String lastName; @JsonIgnore private List<String> technologies = new ArrayList<>(); public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public List<String> getTechnologies() { return technologies; } public void setTechnologies(List<String> technologies) { this.technologies = technologies; } }
Output
{ "EMPLOYEE_FIRST_NAME" : "Raja", "EMPLOYEE_LAST_NAME" : "Ramesh", "EMPLOYEE_ID" : 120 }
- Related Articles
- How to ignore a class during the serialization using Jackson in Java?
- How to implement custom JSON serialization with Gson in Java?
- How to exclude a field in Gson during serialization in Java?
- How can we read a JSON file in Java?
- How can we merge two JSON objects in Java?
- How can we merge two JSON arrays in Java?
- How can we encode a JSON object in Java?
- How can we decode a JSON object in Java?
- How to ignore the multiple properties of a JSON object in Java?
- How can we convert a JSON string to a JSON object in Java?
- How can we create a JSON using JsonGenerator in Java?
- How can we parse a nested JSON object in Java?
- How to implement custom JSON de-serialization with Gson in Java?\n
- How to control serialization through @JSON annotation using flexjson in Java?\n
- How can we convert a map to the JSON object in Java?

Advertisements