json-path/JsonPath · error · InvalidPathException
Expected character: %c
Error message
Expected character: %c
What it means
readSignificantChar skips insignificant whitespace and then requires the next significant character in the path to equal the expected character c. If it differs, the path syntax is invalid at that position and an InvalidPathException with 'Expected character: %c' is thrown. This is a strict syntax check used while tokenizing path segments.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/CharacterIndex.java:222
public char nextSignificantChar() {
return nextSignificantChar(position);
}
public char nextSignificantChar(int startPosition) {
int readPosition = startPosition + 1;
while (!isOutOfBounds(readPosition) && charAt(readPosition) == SPACE) {
readPosition++;
}
if (!isOutOfBounds(readPosition)) {
return charAt(readPosition);
} else {
return ' ';
}
}
public void readSignificantChar(char c) {
if (skipBlanks().currentChar() != c) {
throw new InvalidPathException(String.format("Expected character: %c", c));
}
incrementPosition(1);
}
public boolean hasSignificantSubSequence(CharSequence s) {
skipBlanks();
if (! inBounds(position + s.length() - 1)) {
return false;
}
if (! subSequence(position, position + s.length()).equals(s)) {
return false;
}
incrementPosition(s.length());
return true;
}
public int indexOfPreviousSignificantChar(int startPosition){View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Read the message to see which character was expected, then inspect the path around that position and insert/correct the delimiter.
- Validate the whole path with JsonPath.compile() and fix the reported syntax error.
- Replace fancy Unicode quotes/spaces pasted from editors or web pages with plain ASCII equivalents.
- Build paths with constants or a builder instead of ad-hoc string concatenation to avoid malformed expressions.
Example fix
// before
String path = "$[?@.price < 10]"; // missing '(' after '?'
// after
String path = "$[?(@.price < 10)]"; Defensive patterns
Strategy: validation
Validate before calling
String normalized = path.replace('\u00A0', ' ').replace('\u201C', '"').replace('\u201D', '"');
JsonPath.compile(normalized); // throws InvalidPathException on any missing delimiter Try / catch
try {
return JsonPath.compile(path);
} catch (InvalidPathException e) {
// e.getMessage() names the expected character, e.g. "Expected character: )"
throw new IllegalArgumentException("Syntax error in path '" + path + "': " + e.getMessage(), e);
} Prevention
- Use JsonPath.compile() to validate user-supplied paths before evaluation
- Replace smart quotes and non-breaking spaces copied from editors/web pages
- Build paths from constants or builders, not loose string concatenation
- Check balanced parens/brackets with a simple counter before compiling
When it happens
Trigger: Parsing a malformed path where a mandatory delimiter is missing or replaced — e.g. a filter lacking its opening or closing parenthesis, a missing '.' between segments, or a stray character where a bracket/paren/dot was expected.
Common situations: Typos in hand-written paths like "$.store.book[?(@.price<10" (missing closing paren); concatenating segments without the correct separator; locale-invisible characters such as non-breaking spaces or smart quotes replacing ASCII delimiters in copied code.
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 :
- Filter must start with '[' and end with ']'.
- 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/a0f4e7aac7113b14.
Report an issue: GitHub.