How to Parse JSON in Java
Java has two dominant JSON libraries: Jackson (industry standard, Spring Boot default) and Gson (Google). This guide covers both with POJO deserialization, error handling, and serialization examples.
Jackson — ObjectMapper
Maven: com.fasterxml.jackson.core:jackson-databind
import com.fasterxml.jackson.databind.ObjectMapper;
// Define a POJO
public class User {
private String name;
private int age;
private boolean active;
// getters and setters (or use @JsonProperty)
}
ObjectMapper mapper = new ObjectMapper();
// Deserialize JSON string to object
String json = "{"name":"Alice","age":30,"active":true}";
User user = mapper.readValue(json, User.class);
System.out.println(user.getName()); // Alice
System.out.println(user.getAge()); // 30
// Deserialize to List
String jsonArray = "[{"name":"Alice"},{"name":"Bob"}]";
List<User> users = mapper.readValue(
jsonArray,
mapper.getTypeFactory().constructCollectionType(List.class, User.class)
);
// Deserialize to Map (no POJO)
Map<String, Object> map = mapper.readValue(json, Map.class);
// Serialize object to JSON string
String output = mapper.writeValueAsString(user);
// Pretty-print
String pretty = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(user);Gson (Google)
Maven: com.google.code.gson:gson
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;
Gson gson = new Gson();
// Deserialize
String json = "{"name":"Alice","age":30}";
User user = gson.fromJson(json, User.class);
// Deserialize a List
String jsonArray = "[{"name":"Alice"},{"name":"Bob"}]";
Type listType = new TypeToken<List<User>>(){}.getType();
List<User> users = gson.fromJson(jsonArray, listType);
// Serialize
String output = gson.toJson(user);
// Pretty-print
Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
String pretty = prettyGson.toJson(user);Error Handling
// Jackson error handling
try {
User user = mapper.readValue(json, User.class);
} catch (JsonParseException e) {
System.err.println("JSON syntax error: " + e.getMessage());
} catch (JsonMappingException e) {
System.err.println("Mapping error: " + e.getMessage());
} catch (IOException e) {
System.err.println("IO error: " + e.getMessage());
}
// Gson error handling
try {
User user = gson.fromJson(json, User.class);
} catch (com.google.gson.JsonSyntaxException e) {
System.err.println("JSON syntax error: " + e.getMessage());
}Parse JSON in Other Languages
FAQ
How do I parse JSON in Java?expand_more
The most common approach is Jackson: ObjectMapper mapper = new ObjectMapper(); User user = mapper.readValue(jsonString, User.class); Alternatively, use Gson: Gson gson = new Gson(); User user = gson.fromJson(jsonString, User.class);
What is the difference between Jackson and Gson for JSON parsing in Java?expand_more
Jackson is the most widely used Java JSON library — faster, more feature-rich, and the default for Spring Boot. Gson is from Google, simpler to configure for basic cases, and does not require annotations. For enterprise Java or Spring projects, Jackson is preferred.
How do I add Jackson to my Java project?expand_more
Add the Maven dependency: <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.17.0</version></dependency>. For Gradle: implementation "com.fasterxml.jackson.core:jackson-databind:2.17.0"
Part of the JSON Toolkit
Explore All JSON Tools
Free online tools for every JSON task — format, validate, convert, compare, and more.