Working with JSON is a common task in modern Java development, especially when dealing with APIs and external data sources. A typical use case involves converting a JSON array (e.g., from a REST API response) into a Java List for further processing.
In this tutorial, we’ll explore multiple ways to convert a JSON array to a Java List, including a detailed section with examples to convert JSON array to Java List using popular libraries such as Jackson, Gson, and org.json. We’ll also discuss when to use each method with example code.
Table of Contents
1. What is a JSON Array?
A JSON array is an ordered list of values enclosed in square brackets ([]). These values can be strings, numbers, objects, or even other arrays.
For example:
[
{"id": 1, "name": "Radha"},
{"id": 2, "name": "Krishnan"},
{"id": 3, "name": "Shyam"}
]
Our goal is to convert this JSON array into a Java List of objects (e.g., List<User>).
2. Create a Java Model Class
Before parsing, let’s define a simple model class:
public class User {
private int id;
private String name;
// Getters and Setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override
public String toString() {
return "User{id=" + id + ", name='" + name + "'}";
}
}
3. Convert JSON Array to Java List Using Jackson
Jackson is one of the most popular libraries for JSON parsing in Java. It provides the ObjectMapper class, which makes JSON serialization and deserialization simple.
🔹 Example Code:
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.List;
public class JacksonExample {
public static void main(String[] args) throws IOException {
String jsonArray = "[{\"id\":1,\"name\":\"Radha\"},{\"id\":2,\"name\":\"Krishnan\"}]";
ObjectMapper mapper = new ObjectMapper();
List<User> users = mapper.readValue(jsonArray, new TypeReference<List<User>>() {});
users.forEach(System.out::println);
}
}
Output:
User{id=1, name='Radha'}
User{id=2, name='Krishnan'}
Why Use Jackson?
- Easy to use and well-documented
- Supports advanced features like annotations
- Great for large-scale Spring Boot applications
4. Convert JSON Array to Java List Using Gson
Gson (by Google) is another lightweight and popular library for JSON operations in Java.
🔹 Example Code:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
public class GsonExample {
public static void main(String[] args) {
String jsonArray = "[{\"id\":1,\"name\":\"Radha\"},{\"id\":2,\"name\":\"Krishnan\"}]";
Gson gson = new Gson();
Type listType = new TypeToken<List<User>>() {}.getType();
List<User> users = gson.fromJson(jsonArray, listType);
users.forEach(System.out::println);
}
}
Output:
User{id=1, name='Radha'}
User{id=2, name='Krishnan'}
Why Use Gson?
- Simple and fast for basic use cases
- Perfect for Android apps or lightweight Java projects
5. Convert JSON Array to Java List Using org.json
The org.json library provides a JSONArray class for manual JSON parsing. Though it’s not as elegant as Jackson or Gson, it can be useful in small projects or legacy systems.
🔹 Example Code:
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class OrgJsonExample {
public static void main(String[] args) {
String jsonArray = "[{\"id\":1,\"name\":\"Radha\"},{\"id\":2,\"name\":\"Krishnan\"}]";
JSONArray array = new JSONArray(jsonArray);
List<User> users = new ArrayList<>();
for (int i = 0; i < array.length(); i++) {
JSONObject obj = array.getJSONObject(i);
User user = new User();
user.setId(obj.getInt("id"));
user.setName(obj.getString("name"));
users.add(user);
}
users.forEach(System.out::println);
}
}
Output:
User{id=1, name='Radha'}
User{id=2, name='Krishnan'}
Why Use org.json?
- Good for small projects or when working without external dependencies
- Offers low-level JSON manipulation control
6. Conclusion
Converting a JSON array to a Java List is straightforward with modern JSON libraries. Here’s a quick comparison:
| Library | Code Simplicity | Recommended For |
|---|---|---|
| Jackson | ⭐⭐⭐⭐⭐ | Spring Boot, enterprise apps |
| Gson | ⭐⭐⭐⭐ | Android, lightweight apps |
| org.json | ⭐⭐ | Simple or legacy codebases |
Final Thoughts
If you’re working with Spring Boot or enterprise-level applications, Jackson is the best choice. For simpler use cases or Android apps, Gson works great.