6 Best Java JSON Parsing Libraries Compared: Jackson, Gson, FastJSON, and More
This article compares six popular Java JSON parsing solutions—Jackson, Gson, FastJSON, JsonPath, org.json, and manual parsing—detailing their features, code examples, advanced options, and pros and cons to help developers choose the right tool for various scenarios.
Introduction
In Java development, parsing JSON is a common requirement for interacting with front‑ends, third‑party APIs, and configuration files. This article summarizes six mainstream JSON parsing methods.
1. Using Jackson: Industry Standard
Features
Powerful serialization and deserialization : Convert JSON strings to Java objects and vice‑versa.
Support for complex structures : Handles nested objects, arrays, generics, etc.
Annotation support : @JsonIgnore, @JsonProperty and others allow fine‑grained control.
High performance : Widely used in enterprise projects.
Code Example
<code>import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonExample {
public static void main(String[] args) throws Exception {
String json = "{\"id\":1,\"name\":\"Zhang San\"}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println(user.getName()); // Output: Zhang San
}
}
class User {
private int id;
private String name;
// Getters and setters omitted
}
</code> <code>User user = new User();
user.setId(1);
user.setName("Li Si");
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json); // Output: {"id":1,"name":"Li Si"}
</code>Advanced Features
Date formatting : @JsonFormat(pattern = "yyyy-MM-dd")
Ignore fields : @JsonIgnore
Rename fields : @JsonProperty("custom_name")
Pros and Cons
Pros: Comprehensive features, supports complex scenarios, high performance, active community.
Cons: Many configuration options, higher learning curve, large library size.
2. Using Gson: Lightweight and Easy
Features
Lightweight : Minimal code, suitable for small‑to‑medium projects.
Generic support : Handles JSON with generic types.
Annotation control : @Expose to control serialization.
Extensible : Custom serializers/deserializers for complex cases.
Code Example
<code>import com.google.gson.Gson;
public class GsonExample {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"Wang Wu\"}";
Gson gson = new Gson();
User user = gson.fromJson(json, User.class);
System.out.println(user.getName()); // Output: Wang Wu
}
}
</code> <code>User user = new User();
user.setId(2);
user.setName("Zhao Liu");
Gson gson = new Gson();
String json = gson.toJson(user);
System.out.println(json); // Output: {"id":2,"name":"Zhao Liu"}
</code>Advanced Features
Ignore fields : @Expose
Custom serializer/deserializer via GsonBuilder.registerTypeAdapter()
Pros and Cons
Pros: Lightweight, easy to learn, good extensibility.
Cons: Slightly lower performance than Jackson, fewer features.
3. Using FastJSON: High Performance
Features
Excellent performance : Very fast parsing, suitable for large data volumes.
Dynamic field support : Handles dynamic JSON structures.
Rich type support : Nested objects, generics, arrays.
Annotation control : Similar to Jackson and Gson.
Code Example
<code>import com.alibaba.fastjson.JSON;
public class FastJsonExample {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"Xiao Ming\"}";
User user = JSON.parseObject(json, User.class);
System.out.println(user.getName()); // Output: Xiao Ming
}
}
</code> <code>User user = new User();
user.setId(3);
user.setName("Xiao Hong");
String json = JSON.toJSONString(user);
System.out.println(json); // Output: {"id":3,"name":"Xiao Hong"}
</code>Advanced Features
Automatic camelCase to snake_case
Dynamic field parsing : JSON.parseObject(json, Map.class)
Pros and Cons
Pros: Extremely fast, handles complex dynamic fields, comprehensive features.
Cons: Historical security concerns, community activity slightly lower than Jackson.
4. Using JsonPath: Quick Nested Field Extraction
Features
Efficient field extraction : XPath‑like expressions for nested data.
Strong flexibility : Supports dynamic fields and conditional filters.
Lightweight : Focused solely on extraction.
Code Example
<code>import com.jayway.jsonpath.JsonPath;
public class JsonPathExample {
public static void main(String[] args) {
String json = "{\"store\":{\"book\":[{\"title\":\"Book1\",\"price\":10},{\"title\":\"Book2\",\"price\":20}]}}";
String title = JsonPath.read(json, "$.store.book[0].title");
System.out.println(title); // Output: Book1
List<Integer> prices = JsonPath.read(json, "$.store.book[*].price");
System.out.println(prices); // Output: [10, 20]
}
}
</code>Pros and Cons
Pros: Simple and efficient field extraction, strong dynamic handling.
Cons: Does not support full serialization/deserialization.
5. Using org.json: Simple Utility Class
Features
Lightweight : Single utility class for simple scenarios.
Easy construction and parsing : Quick JSON creation or field extraction.
Limited flexibility : Not suitable for complex object mapping.
Code Example
<code>import org.json.JSONObject;
public class OrgJsonExample {
public static void main(String[] args) {
String json = "{\"id\":1,\"name\":\"Zhang San\"}";
JSONObject jsonObject = new JSONObject(json);
System.out.println(jsonObject.getString("name")); // Output: Zhang San
JSONObject newJson = new JSONObject();
newJson.put("id", 2);
newJson.put("name", "Li Si");
System.out.println(newJson.toString()); // Output: {"id":2,"name":"Li Si"}
}
}
</code>Pros and Cons
Pros: Lightweight, easy to use, low learning cost.
Cons: Limited functionality, poor extensibility.
6. Manual JSON Parsing: Maximum Flexibility
Features
Complete freedom : No third‑party library dependency.
Dynamic handling : Suitable for irregular JSON structures.
Higher code complexity : Best for special cases.
Code Example
<code>import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
public class ManualParsing {
public static void main(String[] args) throws Exception {
String json = "{\"id\":1,\"name\":\"Dynamic Field\"}";
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> map = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
System.out.println(map.get("name")); // Output: Dynamic Field
}
}
</code>Pros and Cons
Pros: High flexibility, no external dependencies.
Cons: Complex code, lower performance compared to dedicated libraries.
Conclusion
The six methods each have distinct strengths and weaknesses, making the choice dependent on project requirements such as performance, complexity, and maintainability.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.