json-path/JsonPath · error · InvalidPathException

Square brackets does not match in filter

Error message

Square brackets does not match in filter 

What it means

Raised in FilterCompiler.readPath while scanning a path operand ($... or @...) inside a filter: the compiler found '[' in the path but never the matching ']' (or the brackets are improperly nested/escaped), so the bracket segment is unbalanced. The offending input is a filter path with an unclosed bracket, e.g. $.items[?(@.x == 1] or $store[0.

Source

Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java:362

        if(!boolValue.equals("true") && !boolValue.equals("false")){
            throw new InvalidPathException("Expected boolean literal");
        }
        filter.incrementPosition(boolValue.length());
        logger.trace("BooleanLiteral from {} to {} -> [{}]", begin, end, boolValue);

        return ValueNode.createBooleanNode(boolValue);
    }

    private PathNode readPath() {
        char previousSignificantChar = filter.previousSignificantChar();
        int begin = filter.position();

        filter.incrementPosition(1); //skip $ and @
        while (filter.inBounds()) {
            if (filter.currentChar() == OPEN_SQUARE_BRACKET) {
                int closingSquareBracketIndex = filter.indexOfMatchingCloseChar(filter.position(), OPEN_SQUARE_BRACKET, CLOSE_SQUARE_BRACKET, true, false);
                if (closingSquareBracketIndex == -1) {
                    throw new InvalidPathException("Square brackets does not match in filter " + filter);
                } else {
                    filter.setPosition(closingSquareBracketIndex + 1);
                }
            }
            boolean closingFunctionBracket = (filter.currentChar() == CLOSE_PARENTHESIS && currentCharIsClosingFunctionBracket(begin));
            boolean closingLogicalBracket  = (filter.currentChar() == CLOSE_PARENTHESIS && !closingFunctionBracket);

            if (!filter.inBounds() || isRelationalOperatorChar(filter.currentChar()) || filter.currentChar() == SPACE || closingLogicalBracket) {
                break;
            } else {
                filter.incrementPosition(1);
            }
        }

        boolean shouldExists = !(previousSignificantChar == NOT);
        CharSequence path = filter.subSequence(begin, filter.position());
        return ValueNode.createPathNode(path, false, shouldExists);
    }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Close the bracket segment with a matching ']', e.g. $.store[0] or $.items[?(@.x == 1)]
  2. Check for brackets inside string literals within the filter that the scanner may treat as segment openers — quote/escape them correctly
  3. Avoid mixing filter syntax with path syntax incorrectly; put operators only inside [?( ... )] segments
  4. Pre-validate bracket balance in dynamically built path strings before calling JsonPath.compile/parse
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java:362 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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