JamesNK/Newtonsoft.Json · error · JsonException
Unexpected end while parsing path.
Error message
Unexpected end while parsing path.
What it means
Thrown by the JPath parser when the path ends (or a query boundary is reached) immediately after a dot, with no field name following it. ParsePath detects the followingDot flag combined with the end of the base path and rejects the dangling separator.
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:194
}
bool atPathEnd = (_currentIndex == _expression.Length);
if (_currentIndex > currentPartStartIndex)
{
string? member = _expression.Substring(currentPartStartIndex, _currentIndex - currentPartStartIndex).TrimEnd();
if (member == "*")
{
member = null;
}
filters.Add(CreatePathFilter(member, scan));
}
else
{
// no field name following dot in path and at end of base path/query
if (followingDot && (atPathEnd || query))
{
throw new JsonException("Unexpected end while parsing path.");
}
}
return atPathEnd;
}
private static PathFilter CreatePathFilter(string? member, bool scan)
{
PathFilter filter = (scan) ? (PathFilter)new ScanFilter(member) : new FieldFilter(member);
return filter;
}
private PathFilter ParseIndexer(char indexerOpenChar, bool scan)
{
_currentIndex++;
char indexerCloseChar = (indexerOpenChar == '[') ? ']' : ')';
View on GitHub (pinned to 4f73e74372)
Solutions
- Remove the trailing dot from the path.
- When building paths dynamically, append '.' only between segments, never after the last one.
- Trim trailing dots from the final expression string before passing it to SelectToken.
Example fix
// before var path = "$.data."; token.SelectToken(path); // after var path = "$.data"; token.SelectToken(path);
Defensive patterns
Strategy: validation
Validate before calling
// Trim trailing dots/separators before querying
path = path.TrimEnd('.');
token.SelectToken(path); Try / catch
try
{
token.SelectToken(path);
}
catch (JsonException ex) when (ex.Message.Contains("Unexpected end while parsing path"))
{
path = path.TrimEnd('.');
token.SelectToken(path);
} Prevention
- Trim trailing dots from any path before passing it to SelectToken.
- When appending segments in a loop, add '.' only between items.
- Validate that no path segment is empty after splitting on '.'.
When it happens
Trigger: A path like token.SelectToken("$.data.") or "$.." where a dot is the last meaningful token. ParsePath.cs:192-194 fires because followingDot is true and atPathEnd is true with no member text.
Common situations: Trailing dots left by string trimming or concatenation bugs; building a path in a loop that appends '.' unconditionally; a recursive '..' with nothing after it.
Related errors
- Unexpected character while parsing path:
- Unexpected character following indexer:
- 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/032630b8f2b6846f.
Report an issue: GitHub.