JSON Processing in Java: Libraries and Practical Examples

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

JSON Processing in Java: Library Overview and Practical Examples



JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web development, mobile applications, and microservices. In Java, there are several popular libraries for working with JSON. In this article, we will look at three main ones: Jackson, Gson, and org.json, and provide practical examples of parsing, serialization, and JSON generation.



1. Jackson — The Most Popular Library



Jackson (com.fasterxml.jackson.core) is a powerful library with high performance. It supports annotations, streams, and full mapping customization. To add it, include in Maven:



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


Example: Serializing an Object to JSON



import com.fasterxml.jackson.databind.ObjectMapper;import java.util.*;

public class JacksonExample { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper();

// Create an object Map<String, Object> user = new HashMap<>(); user.put("name", "Ivan"); user.put("age", 30); user.put("skills", Arrays.asList("Java", "Spring", "SQL"));

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


Output:



{  "name" : "Ivan",  "age" : 30,  "skills" : [ "Java", "Spring", "SQL" ]}


Example: Parsing JSON into a Java Object



import com.fasterxml.jackson.databind.ObjectMapper;

public class ParseJson { public static void main(String[] args) throws Exception { String json = "{\\"name\\":\\"Anna\\",\\"age\\":25}";

ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(json, User.class);

System.out.println("Name: " + user.getName()); System.out.println("Age: " + user.getAge()); }}

class User { private String name; private int age;

// getters and setters are mandatory 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; }}


2. Gson — Lightweight and Simple Library from Google



Gson (com.google.code.gson) is known for its simple API and minimal configuration. It is suitable for small projects. Maven:



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


Example: Generating JSON from a Map



import com.google.gson.Gson;import com.google.gson.GsonBuilder;import java.util.*;

public class GsonExample { public static void main(String[] args) { Gson gson = new GsonBuilder().setPrettyPrinting().create();

Map<String, Object> data = new LinkedHashMap<>(); data.put("title", "Example"); data.put("completed", false); data.put("tags", List.of("java", "json"));

String json = gson.toJson(data); System.out.println(json); }}


Example: Parsing JSON into a List of Objects



import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import java.lang.reflect.Type;import java.util.*;

public class GsonParseList { public static void main(String[] args) { String json = "[{\\"id\\":1,\\"name\\":\\"Product A\\"},{\\"id\\":2,\\"name\\":\\"Product B\\"}]";\

Recommendations