Working with JSON in Java: Parsing and Serialization for Beginners

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

What is JSON and why is it needed in Java?



JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and easy for programs to process. In modern Java development, JSON is used everywhere: from configuration files to API requests to web services.



Working with JSON in Java involves two main actions:

  • Serialization — converting a Java object into a JSON string.
  • Parsing (Deserialization) — converting a JSON string back into a Java object.


In this tutorial, we will cover how to perform these operations using two popular libraries: Jackson and Gson. The examples are suitable even for beginner developers.



Project Setup and Adding Libraries



To get started, we need a Java project. We will use Maven for dependency management. Create a new Maven project and add the following dependencies to the pom.xml file:



Adding Jackson

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


Adding Gson

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


Both libraries can be used simultaneously in one project. After updating, the Maven dependencies will be downloaded automatically.



Creating a Java Class for the Example



For demonstration, let's create a simple User class that we will serialize and parse:



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

// Empty constructor is required for deserialization public User() {}

public User(String name, int age, String email) { this.name = name; this.age = age; this.email = email; }

// Getters and setters (required for Jackson) 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; }}


Important: For the libraries to work correctly, the class must have:

  • An empty constructor (no arguments).
  • Getters and setters for all fields (required for Jackson; for Gson, getters are sufficient).


Serializing a Java Object to JSON



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



Example with Jackson

import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonExample { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); User user = new User("Anna", 25, "anna@example.com");

// Serialization to string String json = mapper.writeValueAsString(user); System.out.println(json); }}


Execution result:

{"name":"Anna","age":25,"email":"anna@example.com"}


Example with Gson

import com.google.gson.Gson;

public class GsonExample { public static void main(String[] args) { Gson gson = new Gson(); User user = new User("Anna", 25, "anna@example.com");

// Serialization to string String json = gson.toJson(user); System.out.println(json); }}


The result will be identical. Both libraries handle the task equally well.

Blogs

Book Recommendations