JamesNK/Newtonsoft.Json · error · JsonException
Unexpected character while parsing path indexer:
Error message
Unexpected character while parsing path indexer:
What it means
Thrown by the JPath array-indexer parser when a wildcard '*' is followed by a character other than the indexer's closing bracket. After consuming '*' and whitespace, the parser expects ']' (or ')'); anything else (JPath.cs:331-333) is rejected.
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:333
string indexer = _expression.Substring(start, length);
indexes.Add(Convert.ToInt32(indexer, CultureInfo.InvariantCulture));
_currentIndex++;
EatWhitespace();
start = _currentIndex;
end = null;
}
else if (currentCharacter == '*')
{
_currentIndex++;
EnsureLength("Path ended with open indexer.");
EatWhitespace();
if (_expression[_currentIndex] != indexerCloseChar)
{
throw new JsonException("Unexpected character while parsing path indexer: " + currentCharacter);
}
return new ArrayIndexFilter();
}
else if (currentCharacter == ':')
{
int length = (end ?? _currentIndex) - start;
if (length > 0)
{
string indexer = _expression.Substring(start, length);
int index = Convert.ToInt32(indexer, CultureInfo.InvariantCulture);
if (colonCount == 0)
{
startIndex = index;
}
else if (colonCount == 1)View on GitHub (pinned to 4f73e74372)
Solutions
- Use '[*]' alone for a wildcard, or '[0]' for a specific index, never both in one indexer.
- Remove any trailing characters after '*' inside the brackets.
- If you need multiple indices, use a comma list of integers: $[0,1].
Example fix
// before
token.SelectToken("$[*1]");
// after
token.SelectToken("$[*]"); Defensive patterns
Strategy: validation
Validate before calling
// Use '*' only as a standalone wildcard, never mixed with indices
string indexer = wantAll ? "[*]" : $"[{index}]";
string path = $"$.data{indexer}"; Prevention
- Use '[*]' alone or '[0]' alone, never a mix like '[*1]'.
- If you need multiple indices, use a comma list of integers.
- Treat '*' as an exclusive choice within a single indexer.
When it happens
Trigger: A path like token.SelectToken("$[*1]") or "$[*,0]" where '*' is not immediately followed by the closing ']'. The parser advances past '*' then finds an unexpected character.
Common situations: Mixing wildcard and explicit index syntax; typos where '*' is meant as multiplication or a glob; concatenation placing extra content inside a wildcard indexer.
Related errors
- Array index expected.
- Index * not valid on {0}.
- Unexpected character while parsing path:
- Unexpected character following indexer:
- Unexpected end while parsing path.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/65712d14bd0dd369.
Report an issue: GitHub.