Generate Java POJOs with List<T> Fields from JSON Arrays
Convert JSON arrays into typed List<T> fields. Learn how the generator infers the element type from array contents and falls back to List<Object> for mixed arrays.
Detailed Explanation
JSON Arrays as Java List
JSON arrays map directly to java.util.List<T>. The generator inspects the array contents to choose the element type T.
Homogeneous Arrays of Primitives
{
"name": "playlist",
"track_ids": [101, 102, 103]
}
public class Playlist {
private String name;
private List<Integer> trackIds;
// accessors
}
Arrays of Objects
{
"id": 1,
"items": [
{ "sku": "A1", "qty": 2 },
{ "sku": "B2", "qty": 1 }
]
}
public class Order {
private Integer id;
private List<Item> items;
}
public class Item {
private String sku;
private Integer qty;
}
The first element of the array drives the class generation. Subsequent elements are assumed to share the same shape — if they do not, you may need to merge fields manually.
Mixed-Type Arrays
{ "values": [1, "two", true] }
public class Root {
private List<Object> values;
}
When the elements have different types, the generator falls back to List<Object>. You can refine this manually using a sealed interface or wrapper class for type-safe access.
Empty Arrays
{ "tags": [] }
private List<Object> tags;
The element type is unknown, so the generator emits List<Object>. Replace with the correct type after consulting your API contract.
Required Import
The generator automatically adds:
import java.util.List;
Why List and not Array?
Arrays in Java are fixed-size and have awkward generics support (new T[] is a compile error). List<T> integrates cleanly with the Collections framework, streams, and most JSON libraries. Jackson, Gson, and Moshi all deserialize JSON arrays into List by default when the field type is List.
Use Case
Pagination responses ("results": [...], "total": 42), aggregation responses ("items": [...]) and event log payloads all rely on JSON arrays. Mapping them to typed List<T> gives you full IDE support and stream-based processing without casts.