Analysis and Solution for Jackson JsonMappingException When Parsing JSON Arrays

Nov 13, 2025 · Programming · 13 views · 7.8

Keywords: Jackson | JSON Parsing | JsonMappingException | Java Deserialization | Type Mapping

Abstract: This paper provides an in-depth analysis of the JsonMappingException: Can not deserialize instance of ... out of START_ARRAY token error encountered when using the Jackson library for JSON data parsing. Through concrete case studies, it demonstrates the issue of mismatched data structure mapping between JSON and Java objects, offers solutions for correcting JSON format and adjusting Java class structures, and discusses approaches for handling similar errors in different scenarios.

Problem Background and Error Analysis

In Java development, using the Jackson library for JSON data parsing is a common practice. However, deserialization errors frequently occur when the JSON data structure does not match the target Java class definitions. The case study discussed in this paper involves the parsing of geographic coordinate data.

In the original JSON data, the center field was defined as an array structure:

"center" : [
    "latitude" : 38.895111, 
    "longitude" : -77.036667
]

Meanwhile, the corresponding Java class definition expected the center field to be a GeoPoint object:

public class Location {
    public String name;
    public int number;
    public GeoPoint center;
}

public class GeoPoint {
    public double latitude;
    public double longitude;
}

In-depth Analysis of Error Causes

When Jackson encounters a START_ARRAY token during parsing, it expects the beginning of an array type. However, the target field center is defined as a GeoPoint object type in the Java class. This type mismatch causes Jackson to throw a JsonMappingException.

The error message clearly indicates the problem:

com.fasterxml.jackson.databind.JsonMappingException: 
Can not deserialize instance of com.example.GeoPoint out of START_ARRAY token

This shows that Jackson attempted to deserialize an array as a single object but failed due to type incompatibility.

Solutions and Code Implementation

The most straightforward solution is to correct the JSON data structure by changing the center field from an array to an object:

[
    {
        "name" : "New York",
        "number" : "732921",
        "center" : {
            "latitude" : 38.895111, 
            "longitude" : -77.036667
        }
    },
    {
        "name" : "San Francisco",
        "number" : "298732",
        "center" : {
            "latitude" : 37.783333, 
            "longitude" : -122.416667
        }
    }
]

In the modified JSON data, the center field uses curly braces {} to define an object instead of square brackets [] for defining an array. This structure perfectly matches the GeoPoint class definition, allowing Jackson to successfully perform deserialization.

The implementation of the parsing method remains unchanged:

public static List<Location> getLocations(InputStream inputStream) {
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        TypeFactory typeFactory = objectMapper.getTypeFactory();
        CollectionType collectionType = typeFactory.constructCollectionType(
                                            List.class, Location.class);
        return objectMapper.readValue(inputStream, collectionType);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

Analysis of Alternative Solutions

Besides modifying the JSON source data, the Java class structure can also be adjusted to handle array-formatted data. For example, the center field type can be changed to an array:

public class Location {
    public String name;
    public int number;
    public GeoPoint[] center;  // Changed to array type
}

Or using a List collection:

public class Location {
    public String name;
    public int number;
    public List<GeoPoint> center;  // Using List collection
}

The advantage of this approach is that it doesn't require modifying the original JSON data, but it necessitates adjusting business logic to handle multiple coordinate points in the array.

Extended Related Scenarios

Similar type mismatch problems frequently occur in other JSON processing scenarios. The Druid data ingestion failure case mentioned in the reference article demonstrates the same problem pattern:

During Druid batch data ingestion, when a JSON object array is provided, the system expects newline-separated JSON objects rather than array format. This leads to similar parsing errors:

com.fasterxml.jackson.databind.JsonMappingException: 
Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token

This indicates that different systems and libraries may have variations in their JSON format processing requirements, and developers need to understand the specific requirements of each system.

Best Practice Recommendations

To avoid similar JSON parsing issues, the following measures are recommended:

  1. Data Format Validation: Validate JSON data structure before parsing to ensure it meets expectations
  2. Type Mapping Verification: Ensure Java class definitions completely match JSON data structures
  3. Error Handling Optimization: Provide more user-friendly error messages and recovery mechanisms
  4. Documentation Standards: Clearly define API and data format specification requirements

By following these best practices, errors during JSON parsing can be significantly reduced, improving code robustness and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.