json-path/JsonPath · error · InvalidPathException
Pattern not closed. Expected
Error message
Pattern not closed. Expected
What it means
Raised in FilterCompiler.readPattern when compiling an inline regex literal (e.g. /pattern/flags) in a JSON Path filter expression: the scanner reached the end of the path without finding the unescaped closing '/' that terminates the pattern, so the literal is unterminated. It is a strict syntax guard — the offending input is a filter path containing a '/' that opens a regex but never closes it (or the closer is escaped, as in \/).
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java:295
private int endOfFlags(int position) {
int endIndex = position;
char[] currentChar = new char[1];
while (filter.inBounds(endIndex)) {
currentChar[0] = filter.charAt(endIndex);
if (PatternFlag.parseFlags(currentChar) > 0) {
endIndex++;
continue;
}
break;
}
return endIndex;
}
private PatternNode readPattern() {
int begin = filter.position();
int closingIndex = filter.nextIndexOfUnescaped(PATTERN);
if (closingIndex == -1) {
throw new InvalidPathException("Pattern not closed. Expected " + PATTERN + " in " + filter);
} else {
if (filter.inBounds(closingIndex+1)) {
int endFlagsIndex = endOfFlags(closingIndex + 1);
if (endFlagsIndex > closingIndex) {
CharSequence flags = filter.subSequence(closingIndex + 1, endFlagsIndex);
closingIndex += flags.length();
}
}
filter.setPosition(closingIndex + 1);
}
CharSequence pattern = filter.subSequence(begin, filter.position());
logger.trace("PatternNode from {} to {} -> [{}]", begin, filter.position(), pattern);
return ValueNode.createPatternNode(pattern);
}
private StringNode readStringLiteral(char endChar) {
int begin = filter.position();
View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Close the regex literal with an unescaped '/' in the filter expression, e.g. @.name =~ /^ab.*c$/
- If the pattern must contain a literal '/', escape it as \/ so it is not treated as the closing delimiter
- If the value is not meant to be a regex, replace the '~=' operator and slash-delimited form with a plain string comparison
- Validate the filter path string (balanced, unescaped delimiters) before passing it to 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:295 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/154dd90cc1dd8db4.
Report an issue: GitHub.