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

Working with JSON in Java: Parsing and Serialization for Beginners

\n\n

JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web development, mobile applications, and microservices. For a Java developer, the ability to read (parse) and create (serialize) JSON is an essential skill. In this article, we will cover the basics of working with JSON in Java using two of the most popular libraries: Jackson and Gson. The material is designed for beginners, so all examples will be as simple and clear as possible.

\n\n

What are JSON Parsing and Serialization?

\n

Before diving into the code, let's define the concepts:

\n
    \n
  • Serialization is the process of converting a Java object into a JSON string. For example, you create a User object and want to send it to a server as JSON.
  • \n
  • Parsing (Deserialization) is the reverse process: converting a JSON string back into a Java object. For example, you receive a response from an API and want to work with the data as a regular object.
  • \n
\n\n

Project Setup: Adding Dependencies

\n

To work with JSON, we need external libraries. Let's look at the two most popular options.

\n\n

Option 1: Jackson (recommended for beginners)

\n

Jackson is a powerful and high-performance library. If you use Maven, add the following to pom.xml:

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

Option 2: Gson (from Google)

\n

Gson is a lightweight and intuitive library. The Maven dependency:

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

Both libraries can be downloaded manually from Maven Central if you are not using a build system.

\n\n

Example Data: User Class

\n

For all examples, we will use a simple User class:

\n
public class User {\n    private String name;\n    private int age;\n    private String email;\n\n    // Empty constructor is required for Jackson and Gson\n    public User() {}\n\n    public User(String name, int age, String email) {\n        this.name = name;\n        this.age = age;\n        this.email = email;\n    }\n\n    // Getters and setters (important for serialization)\n    public String getName() { return name; }\n    public void setName(String name) { this.name = name; }\n\n    public int getAge() { return age; }\n    public void setAge(int age) { this.age = age; }\n\n    public String getEmail() { return email; }\n    public void setEmail(String email) { this.email = email; }\n}
\n\n

Parsing JSON (String → Java Object)

\n

Let's see how to read a JSON string and convert it into a User object.

\n\n

Using Jackson

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

Using Gson

\n
import com.google.gson.Gson;\n\npublic class GsonParsingExample {\n    public static void main(String[] args) {\n        String jsonString = "{ \\"name\\": \\"Maria\\", \\"age\\": 25, \\"email\\": \\"maria@example.com\\" }";\n\n        Gson gson = new Gson();\n        User user = gson.fromJson(json                

Blogs

Book Recommendations