{"record":{"id":"8f965b9e3e723a2a","repo":"json-path/JsonPath","slug":"unexpected-close-bracket-at-character-position","errorCode":null,"errorMessage":"Unexpected close bracket ']' at character position: ","messagePattern":"Unexpected close bracket '\\]' at character position: ","errorType":"exception","errorClass":"InvalidPathException","httpStatus":null,"severity":"error","filePath":"json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java","lineNumber":343,"sourceCode":"                case OPEN_PARENTHESIS:\n                    groupParen++;\n                    break;\n                case OPEN_BRACE:\n                    groupBrace++;\n                    break;\n                case OPEN_SQUARE_BRACKET:\n                    groupBracket++;\n                    break;\n\n                case CLOSE_BRACE:\n                    if (0 == groupBrace) {\n                        throw new InvalidPathException(\"Unexpected close brace '}' at character position: \" + path.position());\n                    }\n                    groupBrace--;\n                    break;\n                case CLOSE_SQUARE_BRACKET:\n                    if (0 == groupBracket) {\n                        throw new InvalidPathException(\"Unexpected close bracket ']' at character position: \" + path.position());\n                    }\n                    groupBracket--;\n                    break;\n\n                // In either the close paren case where we have zero paren groups left, capture the parameter, or where\n                // we've encountered a COMMA do the same\n                case CLOSE_PARENTHESIS:\n                    groupParen--;\n                    //CS304 Issue link: https://github.com/json-path/JsonPath/issues/620\n                    if (0 > groupParen || priorChar == '(') {\n                        parameter.append(c);\n                    }\n                case COMMA:\n                    // In this state we've reach the end of a function parameter and we can pass along the parameter string\n                    // to the parser\n                    if ((0 == groupQuote && 0 == groupBrace && 0 == groupBracket\n                            && ((0 == groupParen && CLOSE_PARENTHESIS == c) || 1 == groupParen))) {\n                        endOfStream = (0 == groupParen);","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/json-path/JsonPath/blob/62a4c9f0f65ba3f625aa0867d64c528ba72d09ec/json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java#L325-L361","documentation":"InvalidPathException thrown by PathCompiler.parseFunctionParameters when a ']' is encountered while scanning a function's argument list but no '[' group is open (groupBracket == 0). The compiler counts bracket nesting; an unmatched ']' inside function arguments makes the path invalid.","triggerScenarios":"Compiling a function-style path like $.x.sum([1,2]) typed with a stray ']' — e.g. $.x.sum(1]] ) or an argument string containing ']' without a preceding '['.","commonSituations":"Hand-written filter expressions, copy-paste of partially edited paths, programmatic path building where index brackets were removed but closers left behind.","solutions":["Remove the unmatched ']' or add the missing '[' inside the function arguments","Re-check the argument list of the function call for stray bracket characters","Compile the path in a guarded helper and surface a clear message for user-supplied paths"],"exampleFix":"// before\nJsonPath.read(json, \"$.prices.avg(1])\")\n// after\nJsonPath.read(json, \"$.prices.avg([1,2])\")","handlingStrategy":"validation","validationCode":"static boolean bracketsBalanced(String path) {\n    int depth = 0;\n    for (char c : path.toCharArray()) {\n        if (c == '[') depth++;\n        if (c == ']' && --depth < 0) return false;\n    }\n    return depth == 0;\n}\nif (!bracketsBalanced(path)) throw new IllegalArgumentException(\"Unbalanced brackets in path: \" + path);","typeGuard":null,"tryCatchPattern":"try {\n    JsonPath.compile(path);\n} catch (InvalidPathException e) {\n    throw new IllegalArgumentException(\"Invalid JSON path: \" + path, e);\n}","preventionTips":["Build bracket segments with helper methods that always emit matched pairs","Compile all externally supplied paths once before query execution","Review hand-written paths for stray closers after edits"],"tags":["json-path","path-compiler","unbalanced-brackets","invalid-path"],"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"}