json-path/JsonPath · error · InvalidPathException

Failed to parse ArrayIndexOperation:

Error message

Failed to parse ArrayIndexOperation: 

What it means

ArrayIndexOperation.parse() validates that an array-index operation string (the content of brackets like [1,2,-3]) contains only digits, commas, spaces, and minus signs. Any other character causes InvalidPathException('Failed to parse ArrayIndexOperation: <operation>').

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/path/ArrayIndexOperation.java:46

        return indexes.size() == 1;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append(Utils.join(",", indexes));
        sb.append("]");

        return sb.toString();
    }

    public static ArrayIndexOperation parse(String operation) {
        //check valid chars
        for (int i = 0; i < operation.length(); i++) {
            char c = operation.charAt(i);
            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

  1. Ensure bracket content is a comma-separated list of integers, e.g. [0,2,-1].
  2. Use filter syntax [?(@.field == 'x')] instead of putting expressions inside plain index brackets.
  3. Sanitize/validate any user-supplied path fragments before compiling with JsonPath.compile().
  4. Catch InvalidPathException and report which bracket segment failed.

Example fix

// before
JsonPath.read(json, "$.items[a,b]"); // invalid chars in index op
// after
JsonPath.read(json, "$.items[0,1]");
Defensive patterns

Strategy: validation

Validate before calling

if (!operation.matches("[-0-9, ]*")) throw new InvalidPathException("Invalid array index operation: " + operation);

Try / catch

try {
    JsonPath.compile(path);
} catch (InvalidPathException e) {
    if (e.getMessage().startsWith("Failed to parse ArrayIndexOperation")) { /* fix bracket content */ }
    throw e;
}

Prevention

When it happens

Trigger: A path segment like [$[?(@.x)])] misuse or bracket content with illegal chars — e.g. [a], [1:2] fragments misrouted to this parser, [1,2;3], or brackets containing letters/colons — reaching the array-index parser during Path compilation.

Common situations: Typo'd filter/index syntax (mixing [?(@..)] with plain index brackets), slicing syntax with unexpected tokens, dynamically built paths where user input flows into brackets, or locale-formatted numbers with separators.

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.

Related errors


AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11). Data as JSON: /api/errors/a95000197f36c919. Report an issue: GitHub.