Parsing JSON in Java: From Zero to Working Code
\n\nHave you ever looked at a JSON response from an API and felt a slight panic? "How do I extract name from here? And what do I do with this nested object?" — familiar questions, right? Don't worry, you're not alone. The topic "How to parse JSON in Java" is one of the most popular queries on Stack Overflow, and today we will settle this question once and for all.
JSON (JavaScript Object Notation) is a standard data interchange format. Almost every modern service — from weather to stock quotes — returns data in JSON. Being able to parse it competently is a basic skill for a Java developer. We won't read boring specifications. Instead, we'll take three of the most popular libraries: Gson (Google), Jackson (FasterXML), and the classic org.json. You will see the difference in approaches and will be able to choose your tool.
\n\nWhat will you get after reading? You will be able to parse any JSON — from a simple string to a complex nested structure with arrays. Let's go!
\n\nPreparation: What We'll Need
\n\nFor the examples, we will use a simple Maven project. If you don't use Maven — no problem, just download the library JAR files manually. Here are the minimal dependencies for pom.xml:
<!-- Gson -->\n<dependency>\n <groupId>com.google.code.gson</groupId>\n <artifactId>gson</artifactId>\n <version>2.10.1</version>\n</dependency>\n\n<!-- Jackson -->\n<dependency>\n <groupId>com.fasterxml.jackson.core</groupId>\n <artifactId>jackson-databind</artifactId>\n <version>2.15.2</version>\n</dependency>\n\n<!-- org.json (simple) -->\n<dependency>\n <groupId>org.json</groupId>\n <artifactId>json</artifactId>\n <version>20231013</version>\n</dependency>\n\n\nNow, let's imagine we received the following JSON from the server (user information):
\n\n{\n "id": 42,\n "name": "Alice",\n "email": "alice@example.com",\n "isActive": true,\n "address": {\n "city": "Moscow",\n "street": "Tverskaya"\n },\n "hobbies": ["reading", "programming", "cycling"]\n}\n\n\nOur task is to turn this string into Java objects and extract the necessary fields. Let's go in order.
\n\nMethod 1: Parsing JSON in Java with Gson (Simple and Elegant)
\n\nGson from Google is perhaps the simplest way to parse JSON in Java. Its main advantage is minimalism. You don't need to write a lot of code.
\n\nStep 1. Create a Java class (POJO) that matches the JSON structure.
\n\npublic class User {\n private int id;\n private String name;\n private String email;\n private boolean isActive;\n private Address address;\n private List<String> hobbies;\n\n // Getters and setters (mandatory for Gson!)\n public int getId() { return id; }\n public void setId(int id) { this.id = id; }\n\n public String getName() { return name; }\n public void setName(String name) { this.name = name; }\n\n // ... remaining getters/setters similarly\n}\n\npublic class Address {\n private String city;\n private String street;\n // getters/setters\n}\n\n\nStep 2. Parse JSON with a single line of code.
\n\nimport com.google.gson.Gson;\n\npublic class GsonExample {\n public static void main(String[] args) {\n String json = "{\\"id\\":42,\\"name\\":\\"Alice\\",\\"email\\":\\"alice@example.com\\",\\"isActive\\":true,\\"address\\":{\\"city\\":\\"Moscow\\",\\"street\\":\\"Tverskaya\\"},\\"hobbies\\":[\\"reading\\",\\"programming\\",\\"cycling\\"]}";\n\n Gson gson = new Gson();\n User user = gson.fromJson(json, User.class);\n\n System.out.print