json-path/JsonPath · error · InvalidPathException
Expected wildcard token to end with ']' on position
Error message
Expected wildcard token to end with ']' on position
What it means
InvalidPathException thrown by readWildCardToken when a wildcard '*' inside brackets is not immediately followed by a closing ']'. A bracketed wildcard like [*] must be terminated by ']' right after the '*'; any other character makes the path unparseable.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/path/PathCompiler.java:505
//
// [*]
// *
//
private boolean readWildCardToken(PathTokenAppender appender) {
boolean inBracket = path.currentCharIs(OPEN_SQUARE_BRACKET);
if (inBracket && !path.nextSignificantCharIs(WILDCARD)) {
return false;
}
if (!path.currentCharIs(WILDCARD) && path.isOutOfBounds(path.position() + 1)) {
return false;
}
if (inBracket) {
int wildCardIndex = path.indexOfNextSignificantChar(WILDCARD);
if (!path.nextSignificantCharIs(wildCardIndex, CLOSE_SQUARE_BRACKET)) {
int offset = wildCardIndex + 1;
throw new InvalidPathException("Expected wildcard token to end with ']' on position " + offset);
}
int bracketCloseIndex = path.indexOfNextSignificantChar(wildCardIndex, CLOSE_SQUARE_BRACKET);
path.setPosition(bracketCloseIndex + 1);
} else {
path.incrementPosition(1);
}
appender.appendPathToken(PathTokenFactory.createWildCardPathToken());
return path.currentIsTail() || readNextToken(appender);
}
//
// [1], [1,2, n], [1:], [1:2], [:2]
//
private boolean readArrayToken(PathTokenAppender appender) {
if (!path.currentCharIs(OPEN_SQUARE_BRACKET)) {View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Ensure every bracketed wildcard is written as [*] with a closing bracket
- Check for characters between '*' and ']' in the path string
- Validate the assembled path with JsonPath.compile() before use
Example fix
// before JsonPath.read(json, "$..book[*") // after JsonPath.read(json, "$..book[*]")
Defensive patterns
Strategy: validation
Validate before calling
static boolean wildcardsClosed(String path) {
int i = 0;
while ((i = path.indexOf('*', i)) != -1) {
if (i > 0 && path.charAt(i - 1) == '[') {
int j = i + 1;
while (j < path.length() && Character.isWhitespace(path.charAt(j))) j++;
if (j >= path.length() || path.charAt(j) != ']') return false;
}
i++;
}
return true;
} Try / catch
try {
return JsonPath.read(json, path);
} catch (InvalidPathException e) {
if (e.getMessage().startsWith("Expected wildcard token")) {
throw new IllegalArgumentException("Wildcard '*' must be written as [*] in: " + path);
}
throw e;
} Prevention
- Always write bracketed wildcards as the exact three characters [*]
- Use path builder helpers (JsonPath.builder().wildcard()) instead of raw strings
- Compile paths in unit tests to catch syntax regressions
When it happens
Trigger: Compiling paths like "$..book[*x" or "$[* , ?]" — the significant char after '*' is not ']'.
Common situations: Hand-edited paths where the closing bracket was deleted, dynamic concatenation of segments missing ']'.
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
- Could not find matching close quote for %s when parsing : %s
- Could not find matching close for %s when parsing regex in :
- Expected character: %c
- Filter must start with '[' and end with ']'.
- Filter must start with '[?' and end with ']'.
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/d94f791aec243af5.
Report an issue: GitHub.