json-path/JsonPath · error · InvalidPathException

Unexpected close bracket ']' at character position:

Error message

Unexpected close bracket ']' at character position: 

What it means

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.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java:343

                case OPEN_PARENTHESIS:
                    groupParen++;
                    break;
                case OPEN_BRACE:
                    groupBrace++;
                    break;
                case OPEN_SQUARE_BRACKET:
                    groupBracket++;
                    break;

                case CLOSE_BRACE:
                    if (0 == groupBrace) {
                        throw new InvalidPathException("Unexpected close brace '}' at character position: " + path.position());
                    }
                    groupBrace--;
                    break;
                case CLOSE_SQUARE_BRACKET:
                    if (0 == groupBracket) {
                        throw new InvalidPathException("Unexpected close bracket ']' at character position: " + path.position());
                    }
                    groupBracket--;
                    break;

                // In either the close paren case where we have zero paren groups left, capture the parameter, or where
                // we've encountered a COMMA do the same
                case CLOSE_PARENTHESIS:
                    groupParen--;
                    //CS304 Issue link: https://github.com/json-path/JsonPath/issues/620
                    if (0 > groupParen || priorChar == '(') {
                        parameter.append(c);
                    }
                case COMMA:
                    // In this state we've reach the end of a function parameter and we can pass along the parameter string
                    // to the parser
                    if ((0 == groupQuote && 0 == groupBrace && 0 == groupBracket
                            && ((0 == groupParen && CLOSE_PARENTHESIS == c) || 1 == groupParen))) {
                        endOfStream = (0 == groupParen);

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Remove the unmatched ']' or add the missing '[' inside the function arguments
  2. Re-check the argument list of the function call for stray bracket characters
  3. Compile the path in a guarded helper and surface a clear message for user-supplied paths

Example fix

// before
JsonPath.read(json, "$.prices.avg(1])")
// after
JsonPath.read(json, "$.prices.avg([1,2])")
Defensive patterns

Strategy: validation

Validate before calling

static boolean bracketsBalanced(String path) {
    int depth = 0;
    for (char c : path.toCharArray()) {
        if (c == '[') depth++;
        if (c == ']' && --depth < 0) return false;
    }
    return depth == 0;
}
if (!bracketsBalanced(path)) throw new IllegalArgumentException("Unbalanced brackets in path: " + path);

Try / catch

try {
    JsonPath.compile(path);
} catch (InvalidPathException e) {
    throw new IllegalArgumentException("Invalid JSON path: " + path, e);
}

Prevention

When it happens

Trigger: 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 '['.

Common situations: Hand-written filter expressions, copy-paste of partially edited paths, programmatic path building where index brackets were removed but closers left behind.

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