json-path/JsonPath · error · InvalidPathException

Unexpected close brace '}' at character position:

Error message

Unexpected close brace '}' at character position: 

What it means

InvalidPathException thrown by PathCompiler.parseFunctionParameters when a '}' is encountered while parsing a function's argument list but no matching '{' group is open (groupBrace == 0). The compiler tracks brace nesting while scanning function parameters; an unmatched closer means the path expression is malformed.

Source

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

                        groupQuote--;
                    }
                    else {
                        groupQuote++;
                    }
                    break;
                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);
                    }

View on GitHub (pinned to 62a4c9f0f6)

Solutions

  1. Fix the path string so every '{' inside the function arguments is balanced with a '}'
  2. Escape or remove literal brace characters that are not meant as path syntax
  3. Validate user-supplied path strings with a try { JsonPath.compile(p) } before using them in queries

Example fix

// before
JsonPath.read(json, "$.items.concat('{')")
// after
JsonPath.read(json, "$.items.concat('{}')")
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try {
    DocumentContext ctx = JsonPath.parse(json).read(path);
} catch (InvalidPathException e) {
    log.warn("Malformed path: {}", path, e);
}

Prevention

When it happens

Trigger: Compiling a JSON path with a function call (e.g. $..book.length()) whose parameter text contains an unmatched '}', such as $.foo.concat('}', ) or a brace appearing before any opening brace inside the parentheses.

Common situations: Typos in filter expressions, user-supplied path strings from config or HTTP input, template interpolation injecting stray '}' characters into the path.

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