JSON Processing in Java: Overview of Libraries and Tools
JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in modern web applications, REST APIs, configuration files, and databases. In Java, there are several powerful libraries for working with JSON, each with its own features, performance, and ease of use. In this article, we will review the main tools, their capabilities, code examples, and best practices.
1. Jackson (FasterXML Jackson)
Jackson is the most popular and performant library for working with JSON in Java. It supports both streaming processing (SAX-like style) and an object model (DOM style). Jackson is part of Spring Boot and many enterprise applications.
Key Features:
- Serialization/deserialization of Java objects to and from JSON
- Streaming processing of large JSON files (JsonParser, JsonGenerator)
- Support for annotations for fine-tuning (@JsonProperty, @JsonIgnore, @JsonFormat)
- Working with a tree of nodes (JsonNode)
- Support for JSON Schema and validation
Code Example: Basic Jackson Usage
import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.JsonNode;
public class JacksonExample { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper();
// Serialize object to JSON Person person = new Person("Ivan", 30, "ivan@example.com"); String json = mapper.writeValueAsString(person); System.out.println(json); // {"name":"Ivan","age":30,"email":"ivan@example.com"}
// Deserialize from JSON to object String jsonInput = "{\\"name\\":\\"Maria\\",\\"age\\":25}"; Person deserialized = mapper.readValue(jsonInput, Person.class); System.out.println(deserialized.getName()); // Maria
// Working with a tree of nodes JsonNode root = mapper.readTree(jsonInput); System.out.println(root.get("age").asInt()); // 25 }}
class Person { private String name; private int age; private String email;
// Constructors, getters, setters (required for deserialization) public Person() {} public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } // getters and setters...}2. Gson (Google Gson)
Gson is a library from Google known for its simplicity and lightness. It is ideal for small and medium-sized projects where maximum performance is not required. Gson uses reflection for automatic serialization/deserialization.
Features:
- Minimal configuration — works "out of the box"
- Support for annotations (@Expose, @SerializedName)
- Ability to create custom adapters (JsonSerializer, JsonDeserializer)
- Working with collections, arrays, and nested objects
Code Example: Gson in Action
import com.google.gson.Gson;import com.google.gson.GsonBuilder;
public class GsonExample { public static void main(String[] args) { Gson gson = new GsonBuilder() .setPrettyPrinting() // formatted output .create();
// Creating JSON from an object Product product = new Product("Laptop", 899.99, true); String json = gson.toJson(product); System.out.println(json);
// Parsing JSON into an object String jsonInput = "{\\"name\\":\\"Mouse\\",\\"price\\":25.5,\\"available\\":false}"; Product parsed = gson.fromJson(jsonInput, Product.class); System.out.println(parsed.getName()); // Mouse }}
class Product {