json-path/JsonPath · error · InvalidPathException
Failed to parse token in ArrayIndexOperation:
Error message
Failed to parse token in ArrayIndexOperation:
What it means
After character validation, ArrayIndexOperation.parse() splits the bracket content on commas and parses each token with Integer.parseInt via parseInteger(). A non-integer token (empty string, out-of-int-range number, stray spaces-only token like '1,,2' producing an empty token) raises InvalidPathException('Failed to parse token in ArrayIndexOperation: <token>').
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java:63
if (!isDigit(c) && c != ',' && c != ' ' && c != '-') {
throw new InvalidPathException("Failed to parse ArrayIndexOperation: " + operation);
}
}
String[] tokens = COMMA.split(operation, -1);
List<Integer> tempIndexes = new ArrayList<Integer>(tokens.length);
for (String token : tokens) {
tempIndexes.add(parseInteger(token));
}
return new ArrayIndexOperation(tempIndexes);
}
private static Integer parseInteger(String token) {
try {
return Integer.parseInt(token);
} catch (Exception e){
throw new InvalidPathException("Failed to parse token in ArrayIndexOperation: " + token, e);
}
}
}
View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Remove double/leading/trailing commas from the bracket operation.
- Keep each index within the 32-bit int range and within the array bounds.
- Validate tokens with a regex like ^-?\\d+(\\s*,\\s*-?\\d+)*$ before building the path.
- Catch InvalidPathException and log the offending token to locate the malformed segment.
Example fix
// before JsonPath.read(json, "$.items[1,,2]"); // empty token // after JsonPath.read(json, "$.items[1,2]");
Defensive patterns
Strategy: validation
Validate before calling
if (!operation.trim().matches("-?\\d+(\\s*,\\s*-?\\d+)*")) throw new InvalidPathException("Bad index list: " + operation); Try / catch
try {
JsonPath.compile(path);
} catch (InvalidPathException e) {
if (e.getMessage().startsWith("Failed to parse token in ArrayIndexOperation")) { /* fix token: empty or overflow */ }
throw e;
} Prevention
- Avoid double/leading/trailing commas in index lists
- Keep indices within 32-bit int range
- Validate generated index lists with a regex before joining into a path
When it happens
Trigger: Bracket operations like [1,,2] (double comma → empty token), [ 999999999999 ] (overflow beyond int), or [,1] (leading comma) passed to JsonPath.read()/compile().
Common situations: Programmatically generated index lists joined with commas where an empty entry slips in, very large indices copied from other systems, or hand-edited paths with typos.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to parse ArrayIndexOperation:
- Path must start with '$' or '@'
- Path must not end with a '.
- Character '.' on position is not valid.
- Use bracket notion ['my prop'] if your property contains bla
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/64fa375ad221ea45.
Report an issue: GitHub.