Working with JSON in Java: Parsing and Serialization in Practice

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. In Java, working with JSON is an integral part of development: from reading configuration files to interacting with RESTful services. In this article, we will practically explore how to perform parsing (converting JSON to Java objects) and serialization (converting Java objects to JSON) using two of the most popular libraries: Jackson and Gson.

We will look at real code examples, learn how to handle nested structures, and analyze typical errors. By the end of the article, you will be able to confidently work with JSON in any Java project.



1. Main Libraries for Working with JSON

Jackson vs Gson: Which to Choose?

There are several libraries for working with JSON in the Java ecosystem, but two have become the de facto standard:

  • Jackson — a more performant and feature-rich library. Supports annotations, streaming processing, and custom serializers. Recommended for enterprise projects.
  • Gson — a simple and lightweight library from Google. Ideal for small projects and rapid prototyping.

To use either, simply add the dependency to pom.xml (Maven) or build.gradle (Gradle).



Adding Jackson (Maven)

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


Adding Gson (Maven)

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


2. JSON Parsing: From String to Java Object

Parsing is the process of converting a JSON string into a Java object. Let's look at both approaches using a simple User class as an example.



Creating the Data Model

public class User {    private String name;    private int age;    private String email;

// An empty constructor is mandatory! public User() {}

// 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; }}


Parsing with Jackson

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonParsingExample { public static void main(String[] args) throws Exception { String json = "{\\"name\\":\\"Ivan\\",\\"age\\":30,\\"email\\":\\"ivan@example.com\\"}"; ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(json, User.class); System.out.println("Name: " + user.getName()); System.out.println("Age: " + user.getAge()); }}


Parsing with Gson

import com.google.gson.Gson;

public class GsonParsingExample { public static void main(String[] args) { String json = "{\\"name\\":\\"Maria\\",\\"age\\":25,\\"email\\":\\"maria@example.com\\"}"; Gson gson = new Gson(); User user = gson.fromJson(json, User.class); System.out.println("Name: " + user.getName()); System.out.println("Email: " + user.getEmail()); }}


3. Serialization: From Java Object to JSON

Serialization is the reverse process: we take a Java object and convert it into a JSON string. This is necessary, for example

Blogs

Book Recommendations