JamesNK/Newtonsoft.Json · error · JsonException
Unexpected character while parsing path:
Error message
Unexpected character while parsing path:
What it means
Thrown by the JPath parser when, after parsing the main path expression, there is leftover non-whitespace content that the parser could not consume. ParseMain calls ParsePath, and if it returns false (did not reach the end) while trailing characters remain, the unexpected character is reported. This is a JSONPath syntax error detected at path construction time.
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:88
// only increment position for "$." or "$["
// otherwise assume property that starts with $
char c = _expression[_currentIndex + 1];
if (c == '.' || c == '[')
{
_currentIndex++;
currentPartStartIndex = _currentIndex;
}
}
if (!ParsePath(Filters, currentPartStartIndex, false))
{
int lastCharacterIndex = _currentIndex;
EatWhitespace();
if (_currentIndex < _expression.Length)
{
throw new JsonException("Unexpected character while parsing path: " + _expression[lastCharacterIndex]);
}
}
}
private bool ParsePath(List<PathFilter> filters, int currentPartStartIndex, bool query)
{
bool scan = false;
bool followingIndexer = false;
bool followingDot = false;
bool ended = false;
while (_currentIndex < _expression.Length && !ended)
{
char currentChar = _expression[_currentIndex];
switch (currentChar)
{
case '[':View on GitHub (pinned to 4f73e74372)
Solutions
- Inspect the exact character reported in the message and remove or escape it.
- Validate the path against the supported JSONPath grammar (fields, dots, [index], ['field'], [*], [?()], recursive '..').
- When building paths dynamically, sanitize/quote property names with single quotes inside indexers.
Example fix
// before
token.SelectToken("$.data.#foo");
// after
token.SelectToken("$.data.foo"); Defensive patterns
Strategy: validation
Validate before calling
// Validate path syntax against allowed characters before querying
static bool LooksLikeValidPath(string path)
{
foreach (char c in path)
{
if (!(char.IsLetterOrDigit(c) || c == '.' || c == '[' || c == ']' ||
c == '\'' || c == '*' || c == ',' || c == ':' || c == '(' ||
c == ')' || c == '?' || c == '@' || c == '$' || c == ' ' ||
c == '-' || c == '=' || c == '<' || c == '>' || c == '!' ||
c == '|' || c == '&'))
return false;
}
return true;
} Try / catch
try
{
token.SelectToken(path);
}
catch (JsonException ex) when (ex.Message.StartsWith("Unexpected character while parsing path"))
{
// surface a clear error to the caller with the offending path
} Prevention
- Never interpolate untrusted input directly into a JSONPath string.
- Quote property names with single quotes inside indexers.
- Unit-test dynamically built paths against the supported grammar.
When it happens
Trigger: Passing a malformed JSONPath to SelectToken/SelectTokens, e.g. token.SelectToken("$.data.#foo") or "$.data @x" where an illegal character appears that is not a valid path delimiter or field character. The reported character is _expression[lastCharacterIndex].
Common situations: Dynamically building path strings with unescaped special characters; mixing JSONPath syntax with JSON Pointer or other query dialects; copy/paste typos in a path literal.
Related errors
- Unexpected character following indexer:
- Unexpected end while parsing path.
- Path ended with open indexer.
- Array index expected.
- Unexpected character while parsing path indexer:
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/b2db9121441f3f16.
Report an issue: GitHub.