Published on Sun Nov 30 2025 14:20:00 GMT+0000 (Coordinated Universal Time) by Purusothaman Ramanujam
Getting Started with Jackson: JSON Processing in Java
Introduction
JSON (JavaScript Object Notation) has become the de facto standard for data exchange in modern applications. Whether you’re building REST APIs, working with configuration files, or integrating with external services, you’ll likely need to work with JSON data. Jackson is the most popular and powerful JSON processing library for Java, offering excellent performance and a rich feature set.
In this blog post, we’ll explore the basics of Jackson and learn how to serialize Java objects to JSON and deserialize JSON back to Java objects.
What is Jackson?
Jackson is a high-performance JSON processor for Java that provides:
- Object Mapping: Convert Java objects to/from JSON
- Streaming API: Process JSON as a stream for memory efficiency
- Tree Model: Work with JSON as a tree structure
- Data Binding: Automatic conversion between JSON and Java objects
- Annotation Support: Customize serialization/deserialization behavior
Adding Jackson to Your Project
Maven Dependency
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version></dependency>Gradle Dependency
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'Basic Usage
1. Simple Object Serialization
Let’s start with a simple example. First, create a basic Java class:
public class Person { private String name; private int age; private String email;
// Constructors public Person() {}
public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; }
// Getters and Setters public String getName() { return name; } public void setName(String name) { this.name = name; }
public int getAge() { return age; } public void setAge(int age) { this.age = age; }
public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }}Now, let’s serialize a Person object to JSON:
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample { public static void main(String[] args) throws Exception { // Create ObjectMapper instance ObjectMapper mapper = new ObjectMapper();
// Create a Person object Person person = new Person("John Doe", 30, "john@example.com");
// Serialize to JSON string String jsonString = mapper.writeValueAsString(person); System.out.println("Serialized JSON:"); System.out.println(jsonString);
// Output: // {"name":"John Doe","age":30,"email":"john@example.com"} }}2. JSON Deserialization
Now let’s convert JSON back to a Java object:
public class JacksonDeserializationExample { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper();
// JSON string String jsonString = "{\"name\":\"Jane Smith\",\"age\":25,\"email\":\"jane@example.com\"}";
// Deserialize JSON to Person object Person person = mapper.readValue(jsonString, Person.class);
System.out.println("Deserialized Person:"); System.out.println("Name: " + person.getName()); System.out.println("Age: " + person.getAge()); System.out.println("Email: " + person.getEmail()); }}3. Working with Collections
Jackson can easily handle collections and complex objects:
import java.util.List;import java.util.Arrays;
public class CollectionExample { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper();
// List of Person objects List<Person> people = Arrays.asList( new Person("Alice", 28, "alice@example.com"), new Person("Bob", 32, "bob@example.com"), new Person("Charlie", 35, "charlie@example.com") );
// Serialize list to JSON String jsonString = mapper.writeValueAsString(people); System.out.println("Serialized List:"); System.out.println(jsonString);
// Deserialize JSON array back to List List<Person> deserializedPeople = mapper.readValue(jsonString, mapper.getTypeFactory().constructCollectionType(List.class, Person.class));
System.out.println("\nDeserialized People:"); deserializedPeople.forEach(p -> System.out.println(p.getName() + " - " + p.getAge())); }}Common Annotations
Jackson provides powerful annotations to customize serialization and deserialization:
@JsonProperty
Rename fields in JSON:
public class User { @JsonProperty("user_name") private String name;
@JsonProperty("user_age") private int age;
// getters and setters...}@JsonIgnore
Exclude fields from serialization:
public class User { private String name; private int age;
@JsonIgnore private String password; // This won't appear in JSON
// getters and setters...}@JsonFormat
Format dates and numbers:
import java.time.LocalDate;
public class Event { private String title;
@JsonFormat(pattern = "yyyy-MM-dd") private LocalDate date;
// getters and setters...}Error Handling
Jackson provides excellent error handling capabilities:
public class ErrorHandlingExample { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper();
try { // Invalid JSON String invalidJson = "{\"name\":\"John\",\"age\":\"not_a_number\"}"; Person person = mapper.readValue(invalidJson, Person.class); } catch (JsonMappingException e) { System.err.println("Mapping error: " + e.getMessage()); } catch (JsonParseException e) { System.err.println("Parse error: " + e.getMessage()); } catch (Exception e) { System.err.println("Unexpected error: " + e.getMessage()); } }}Best Practices
- Reuse ObjectMapper: ObjectMapper is thread-safe, so create it once and reuse it.
- Handle Exceptions: Always wrap Jackson operations in try-catch blocks.
- Use Annotations Sparingly: Only use annotations when necessary for customization.
- Consider Performance: For high-performance scenarios, consider using Jackson’s streaming API.
- Validate Input: Always validate JSON input before processing.
Conclusion
Jackson is an essential library for any Java developer working with JSON data. Its simple API, excellent performance, and rich feature set make it the go-to choice for JSON processing in Java applications.
In this post, we covered the basics of serialization, deserialization, working with collections, common annotations, and error handling. As you become more comfortable with these concepts, you can explore advanced features like custom serializers, polymorphic deserialization, and the streaming API.
Stay tuned for more advanced Jackson topics in future posts!
Resources
Written by Purusothaman Ramanujam
← Back to blog