Working with JSON in Java: Parsing and Data Serialization

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Working with JSON in Java



JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in modern web applications, APIs, and microservices. Despite its name, JSON is not tied to JavaScript and is well supported in Java. In this article, we will thoroughly examine how to perform parsing, serialization, and deserialization of JSON in Java using popular libraries: Jackson and Gson.



The ability to work with JSON is a mandatory skill for a Java developer. Whether it's reading configuration files, interacting with REST APIs, or transferring data between microservices — JSON is encountered everywhere. We will look at both approaches so you can choose the most suitable one for your project.



Main Libraries for Working with JSON in Java



In the Java ecosystem, there are several popular libraries for working with JSON. Two of them are the de facto standard:



  • Jackson — the most popular library, included in Spring Boot. It features high performance and extensive customization options.
  • Gson — a library from Google, simple to use and lightweight. It is ideal for small projects.


Both libraries support the full cycle of working with JSON: serialization (object → JSON), deserialization (JSON → object), and streaming processing.



Adding Dependencies



To work with the libraries, you need to add them to your project. If you are using Maven, add the following dependencies to pom.xml:



<!-- Jackson --><dependency>    <groupId>com.fasterxml.jackson.core</groupId>    <artifactId>jackson-databind</artifactId>    <version>2.15.2</version></dependency>

<!-- Gson --><dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version></dependency>


For Gradle, add the lines to build.gradle:



implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'implementation 'com.google.code.gson:gson:2.10.1'


Serialization of Java Objects to JSON



Serialization is the process of converting a Java object into a JSON string. Let's look at both approaches.



Serialization using Jackson



Jackson uses the ObjectMapper class for serialization. Example:



import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.core.JsonProcessingException;

public class JacksonSerialization { public static void main(String[] args) throws JsonProcessingException { // Create an object for serialization User user = new User("Ivan", "ivan@example.com", 25); // Create ObjectMapper ObjectMapper mapper = new ObjectMapper(); // Serialize to JSON String json = mapper.writeValueAsString(user); System.out.println(json); // Output: {"name":"Ivan","email":"ivan@example.com","age":25} }}

class User { private String name; private String email; private int age; // Constructor, getters and setters public User(String name, String email, int age) { this.name = name; this.email = email; this.age = age; } public String getName() { return name; } public String getEmail() { return email; } public int getAge() { return age; }}


Serialization using Gson



Gson uses the Gson class with the toJson() method:



import com.google.gson.Gson;

public class GsonSerialization { public static void main(String[] args) { User user = new User("Maria", "maria@example.com", 30); Gson gson = n

Blogs

Book Recommendations