json-path/JsonPath · error · InvalidPathException

Failed to parse SliceOperation:

Error message

Failed to parse SliceOperation: 

What it means

ArraySliceOperation.parse validates that a slice expression (e.g. `1:3`, `:2`, `-1:`) contains only digits, '-', and ':'. Any other character inside the bracket slice triggers InvalidPathException at the character-validation stage, before the expression is even split.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/path/ArraySliceOperation.java:54

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        sb.append(from == null ? "" : from.toString());
        sb.append(":");
        sb.append(to == null ? "" : to.toString());
        sb.append("]");

        return sb.toString();
    }

    public static ArraySliceOperation parse(String operation){
        //check valid chars
        for (int i = 0; i < operation.length(); i++) {
            char c = operation.charAt(i);
            if( !isDigit(c)  && c != '-' && c != ':'){
                throw new InvalidPathException("Failed to parse SliceOperation: " + operation);
            }
        }
        String[] tokens = operation.split(":");

        Integer tempFrom = tryRead(tokens, 0);
        Integer tempTo = tryRead(tokens, 1);
        Operation tempOperation;

        if (tempFrom != null && tempTo == null) {
            tempOperation = Operation.SLICE_FROM;
        } else if (tempFrom != null) {
            tempOperation = Operation.SLICE_BETWEEN;
        } else if (tempTo != null) {
            tempOperation = Operation.SLICE_TO;
        } else {
            throw new InvalidPathException("Failed to parse SliceOperation: " + operation);
        }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Remove any characters from the slice other than digits, '-' and ':', e.g. change `$[1: 3]` to `$[1:3]`.
  2. If you need array-step syntax (`::2`), note this parser does not support it — pre-filter the list yourself after reading it fully.
  3. Validate/normalize dynamic slice strings before embedding them into a path (strip whitespace, sanitize input).

Example fix

// before
JsonPath.read(json, "$[0: 5]"); // space inside slice
// after
JsonPath.read(json, "$[0:5]");
Defensive patterns

Strategy: validation

Validate before calling

boolean isValidSlice(String op) {
    return op != null && op.matches("-?\\d*:-?\\d*");
}

Try / catch

try {
    return JsonPath.read(json, "$[" + slice + "]");
} catch (InvalidPathException e) {
    throw new IllegalArgumentException("Bad slice expression: " + slice, e);
}

Prevention

When it happens

Trigger: Writing a path with a slice containing illegal characters such as `$[1:3,]`, `$[a:b]`, `$[0:2d]`, or whitespace like `$[1 :3]`.

Common situations: Copy-pasting slice syntax from other languages (e.g. Python steps like `[::2]` or commas), template variables interpolated with spaces or units, hand-editing JSON Path strings.

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/bf760dfbd1209eb2. Report an issue: GitHub.