{"record":{"id":"bf760dfbd1209eb2","repo":"json-path/JsonPath","slug":"failed-to-parse-sliceoperation","errorCode":null,"errorMessage":"Failed to parse SliceOperation: ","messagePattern":"Failed to parse SliceOperation: ","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/path/ArraySliceOperation.java","lineNumber":54,"sourceCode":"\n    @Override\n    public String toString() {\n        StringBuilder sb = new StringBuilder();\n        sb.append(\"[\");\n        sb.append(from == null ? \"\" : from.toString());\n        sb.append(\":\");\n        sb.append(to == null ? \"\" : to.toString());\n        sb.append(\"]\");\n\n        return sb.toString();\n    }\n\n    public static ArraySliceOperation parse(String operation){\n        //check valid chars\n        for (int i = 0; i < operation.length(); i++) {\n            char c = operation.charAt(i);\n            if( !isDigit(c)  && c != '-' && c != ':'){\n                throw new InvalidPathException(\"Failed to parse SliceOperation: \" + operation);\n            }\n        }\n        String[] tokens = operation.split(\":\");\n\n        Integer tempFrom = tryRead(tokens, 0);\n        Integer tempTo = tryRead(tokens, 1);\n        Operation tempOperation;\n\n        if (tempFrom != null && tempTo == null) {\n            tempOperation = Operation.SLICE_FROM;\n        } else if (tempFrom != null) {\n            tempOperation = Operation.SLICE_BETWEEN;\n        } else if (tempTo != null) {\n            tempOperation = Operation.SLICE_TO;\n        } else {\n            throw new InvalidPathException(\"Failed to parse SliceOperation: \" + operation);\n        }\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/path/ArraySliceOperation.java#L36-L72","documentation":"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.","triggerScenarios":"Writing a path with a slice containing illegal characters such as `$[1:3,]`, `$[a:b]`, `$[0:2d]`, or whitespace like `$[1 :3]`.","commonSituations":"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.","solutions":["Remove any characters from the slice other than digits, '-' and ':', e.g. change `$[1: 3]` to `$[1:3]`.","If you need array-step syntax (`::2`), note this parser does not support it — pre-filter the list yourself after reading it fully.","Validate/normalize dynamic slice strings before embedding them into a path (strip whitespace, sanitize input)."],"exampleFix":"// before\nJsonPath.read(json, \"$[0: 5]\"); // space inside slice\n// after\nJsonPath.read(json, \"$[0:5]\");","handlingStrategy":"validation","validationCode":"boolean isValidSlice(String op) {\n    return op != null && op.matches(\"-?\\\\d*:-?\\\\d*\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    return JsonPath.read(json, \"$[\" + slice + \"]\");\n} catch (InvalidPathException e) {\n    throw new IllegalArgumentException(\"Bad slice expression: \" + slice, e);\n}","preventionTips":["Only embed digits, '-' and ':' into slice expressions.","Trim/strip whitespace and units from user-provided slice bounds.","Unit-test dynamic path builders with slice inputs."],"tags":["json-path","invalid-path","slice","parse"],"backgroundTag":"invalid-argument-format","analyzedSha":"62a4c9f0f65ba3f625aa0867d64c528ba72d09ec","analyzedAt":"2026-09-11T11:36:00.448Z","contentChangedAt":"2026-09-11T11:36:00.448Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}