JamesNK/Newtonsoft.Json · error · JsonException
Path ended with open query.
Error message
Path ended with open query.
What it means
Thrown inside TryParseExpression when ParsePath returns true (consumed all remaining characters) while parsing the expression's path segment inside a query. The expression started with '$' or '@' and never reached the closing ')' of the query, so the path is truncated mid-query.
Solutions
- Append the missing operator, right-hand value, ')' and ']' to complete the query indexer.
- Add a guard that rejects paths not ending in a balanced set of brackets/parentheses before calling SelectTokens.
Example fix
// before
token.SelectTokens("$..[?(@.name");
// after
token.SelectTokens("$..[?(@.name == 'bob')]"); Defensive patterns
Strategy: validation
Validate before calling
static bool EndsComplete(string path)
{
var trimmed = path.TrimEnd();
return trimmed.EndsWith("]") || trimmed.EndsWith("$") || !trimmed.Contains("[?(");
} Try / catch
try { token.SelectTokens(path).ToList(); }
catch (JsonException ex) when (ex.Message == "Path ended with open query.")
{ /* the query indexer is truncated; reject the input */ } Prevention
- Never emit '[?(' without emitting the matching ')]'.
- Unit-test every generated query path against a sample token.
When it happens
Trigger: A query indexer like [?(...)] whose inner expression path runs to the end of the string without the closing ')' or ']'. Example: token.SelectTokens("$..[?(@.name") truncates after the property name.
Common situations: Truncated path from a clipped string; user input cut off by a length limit; templating that drops the closing delimiters; building a comparison query but forgetting the operator and right-hand side.
Related errors
- Could not read query operator.
- Path ended with an open regex.
- Path ended with an open string.
- Unknown escape character: \
- Index not valid on .
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/6c05a1a341d416a8.
Report an issue: GitHub.
Appendix: source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:454
if (_expression[_currentIndex] == '$')
{
expressionPath = new List<PathFilter> { RootFilter.Instance };
}
else if (_expression[_currentIndex] == '@')
{
expressionPath = new List<PathFilter>();
}
else
{
expressionPath = null;
return false;
}
_currentIndex++;
if (ParsePath(expressionPath!, _currentIndex, true))
{
throw new JsonException("Path ended with open query.");
}
return true;
}
private JsonException CreateUnexpectedCharacterException()
{
return new JsonException("Unexpected character while parsing path query: " + _expression[_currentIndex]);
}
private object ParseSide()
{
EatWhitespace();
if (TryParseExpression(out List<PathFilter>? expressionPath))
{
EatWhitespace();
EnsureLength("Path ended with open query.");View on GitHub (pinned to 4f73e74372)