🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this tutorial, I show you how to ignore certain fields when serializing an object to JSON using Jackson @JsonIgnore, @JsonIgnoreProperties and @JsonIgnoreType annotations with an example. These annotations are used to ignore logical properties in JSON serialization and deserialization.
- @JsonIgnore is used to ignore the logical property used in serialization and deserialization. @JsonIgnore can be used at setter, getter or field.
- @JsonIgnoreProperties ignores the specified logical properties in JSON serialization and deserialization. It is annotated at the class level.
- @JsonIgnoreType is annotated at the class level and it ignores the complete class.
This is very useful when the Jackson defaults aren’t enough and we need to control exactly what gets serialized to JSON. Let's demonstrates the usage of these annotations with examples.
Table of contents
- Maven Dependencies
- Ignore Fields at the Class Level using @JsonIgnoreProperties
- Ignore Field at the Field Level using @JsonIgnore
- Ignore all Fields by Type using @JsonIgnoreType
1. Maven Dependencies
Let’s first add the following dependencies to the pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
- jackson-annotations-2.9.8.jar
- jackson-core-2.9.8.jar
- jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.
2. Ignore Fields at the Class Level using @JsonIgnoreProperties
We can ignore specific fields at the class level, using the @JsonIgnoreProperties annotation and specifying the fields by name:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(value = {
"id",
"firstName"
})
public class CustomerDTO {
private final String id;
private final String firstName;
private final String lastName;
public CustomerDTO(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class IgnoreFieldTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");
String dtoAsString = mapper.writeValueAsString(dtoObject);
System.out.println(dtoAsString);
}
}
Output:
{"lastName":"Stark"}
Note that we have ignored two fields "id" and "firstName" of CustomerDTO and printed only "lastName".
3. Ignore Field at the Field Level using @JsonIgnore
We can also ignore a field directly via the @JsonIgnore annotation directly on the field:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnore;
public class CustomerDTO {
@JsonIgnore
private final String id;
@JsonIgnore
private final String firstName;
private final String lastName;
public CustomerDTO(String id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class IgnoreFieldTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
CustomerDTO dtoObject = new CustomerDTO("CUST100", "Tony", "Stark");
String dtoAsString = mapper.writeValueAsString(dtoObject);
System.out.println(dtoAsString);
}
}
Output:
{"lastName":"Stark"}
Note that we have ignored two fields "id" and "firstName" of CustomerDTO using @JsonIgnore annotation and printed only "lastName".
3. Ignore all Fields by Type using @JsonIgnoreType
We can also ignore all fields of a specified type, using the @JsonIgnoreType annotation. If we control the type, then we can annotate the class directly:
@JsonIgnoreType
public static class Name {
public String firstName;
public String lastName;
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
Here is the complete code:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
public class UserDTO {
public int id;
public Name name;
public UserDTO(int id, Name name) {
super();
this.id = id;
this.name = name;
}
@JsonIgnoreType
public static class Name {
public String firstName;
public String lastName;
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
}
}
Let's test above code with a main() method, note that after the object is written to JSON, the field is indeed not part of the output:
package net.javaguides.jackson.ignore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIgnoreTypeTest {
public static void main(String[] args) throws JsonProcessingException {
UserDTO.Name name = new UserDTO.Name("John", "Doe");
UserDTO user = new UserDTO(1, name);
String result = new ObjectMapper()
.writeValueAsString(user);
System.out.println(result);
}
}
Output:
{"id":1}
Note that the Name class field is ignored by using @JsonIgnoreType annotation.
Related Articles
- Change Field Name in JSON using Jackson (popular)
GitHub Repository
The source code of this article available on my GitHub repository at https://github.com/RameshMF/jackson-json-tutorialMy Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment