Working with JSON in Java: Parsing and Serialization for Beginners
\n\nJSON (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\nWhat are JSON Parsing and Serialization?
\nBefore 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
Userobject 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
Project Setup: Adding Dependencies
\nTo work with JSON, we need external libraries. Let's look at the two most popular options.
\n\nOption 1: Jackson (recommended for beginners)
\nJackson is a powerful and high-performance library. If you use Maven, add the following to pom.xml:
<dependency>\n <groupId>com.fasterxml.jackson.core</groupId>\n <artifactId>jackson-databind</artifactId>\n <version>2.15.2</version>\n</dependency>\n\nOption 2: Gson (from Google)
\nGson 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\nBoth libraries can be downloaded manually from Maven Central if you are not using a build system.
\n\nExample Data: User Class
\nFor all examples, we will use a simple User class:
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\nParsing JSON (String → Java Object)
\nLet's see how to read a JSON string and convert it into a User object.
Using Jackson
\nimport 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\nUsing Gson
\nimport 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