json-path/JsonPath · error · IllegalArgumentException
e
Error message
e
What it means
ValueNode.parse() evaluates an inline JSON literal used as an operand in a filter expression. It uses JSONParser in MODE_PERMISSIVE to parse the raw text; if parsing fails (ParseException), it wraps it in an IllegalArgumentException. This means the JSON literal embedded in the path/filter expression is malformed, so JsonPath cannot turn it into a value for comparison.
Source
Thrown at json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNodes.java:148
}
public JsonNode asJsonNode() {
return this;
}
public ValueNode asValueListNode(Predicate.PredicateContext ctx){
if(!isArray(ctx)){
return UNDEFINED;
} else {
return new ValueListNode(Collections.unmodifiableList((List) parse(ctx)));
}
}
public Object parse(Predicate.PredicateContext ctx){
try {
return parsed ? json : new JSONParser(JSONParser.MODE_PERMISSIVE).parse(json.toString());
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}
public boolean isParsed() {
return parsed;
}
public Object getJson() {
return json;
}
public boolean isArray(Predicate.PredicateContext ctx) {
return parse(ctx) instanceof List;
}
public boolean isMap(Predicate.PredicateContext ctx) {
return parse(ctx) instanceof Map;
}View on GitHub (pinned to 62a4c9f0f6)
Solutions
- Validate/fix the JSON literal embedded in the filter expression; quote strings properly ('value' or "value").
- Print/log the full path string before calling JsonPath.read() and check the literal fragment manually with a JSON linter.
- Use JsonPath's predicate API (Filter.filter(where(...))) instead of string-embedded literals to avoid parsing text.
- Catch IllegalArgumentException from read() to surface a user-facing invalid-path message.
Example fix
// before
List<Map<String,Object>> r = JsonPath.read(json, "$[?(@.x in {a,b})]"); // malformed literal
// after
List<Map<String,Object>> r = JsonPath.read(json, "$[?(@.x in ['a','b'])]"); Defensive patterns
Strategy: validation
Validate before calling
if (!path.matches(".*\\[\\?\\(.*['\"]?[^'\"]*['\"]?.*\\)\\]")) throw new IllegalArgumentException("Suspicious literal in filter: " + path);
// or: assert the embedded literal parses: try { new JSONParser(JSONParser.MODE_PERMISSIVE).parse(literal); } catch (ParseException e) { throw new IllegalArgumentException(e); } Try / catch
try {
JsonPath.read(json, path);
} catch (IllegalArgumentException e) {
throw new InvalidPathException("Malformed literal in path: " + path, e);
} Prevention
- Quote all string literals in filters with single quotes
- Lint generated path strings before use
- Prefer the Filter.where(...) predicate API over string filters
When it happens
Trigger: A filter like [?(@.foo == <literal>)] where <literal> is text the permissive parser still rejects — e.g. unbalanced braces/brackets, invalid escape sequences, or malformed JSON fragments inside the path string — is passed to JsonPath.parse()/read(). parse() is invoked by type(), asValueListNode(), isArray(), isMap(), length(), isEmpty() when the operand node is first evaluated.
Common situations: Hand-written path strings with typos in embedded JSON (e.g. [?(@.x in {'a','b']) single quotes/braces mixups), dynamically concatenated path strings from user input, or copying JSON literals with trailing commas into a predicate.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- Could not convert
- JsonPathException
- InvalidJsonException
- JSON path [%s] doesn't match. Expected: %s Actual: %s
- JSON Assert Error: %s Expected: %s Actual: %s
AI-assisted analysis of json-path/JsonPath@62a4c9f0f6 (2026-09-11).
Data as JSON: /api/errors/69793d3676c7c2ff.
Report an issue: GitHub.