JamesNK/Newtonsoft.Json · error · JsonException
Array index expected.
Error message
Array index expected.
What it means
Thrown by the JPath array-indexer parser when a comma-separated multi-index list reaches its closing bracket but the segment just parsed has zero length, meaning an index value was expected but missing. This is the multi-index variant at JPath.cs:256-260, triggered when indexes != null and length == 0.
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:260
{
char currentCharacter = _expression[_currentIndex];
if (currentCharacter == ' ')
{
end = _currentIndex;
EatWhitespace();
continue;
}
if (currentCharacter == indexerCloseChar)
{
int length = (end ?? _currentIndex) - start;
if (indexes != null)
{
if (length == 0)
{
throw new JsonException("Array index expected.");
}
string indexer = _expression.Substring(start, length);
int index = Convert.ToInt32(indexer, CultureInfo.InvariantCulture);
indexes.Add(index);
return new ArrayMultipleIndexFilter(indexes);
}
else if (colonCount > 0)
{
if (length > 0)
{
string indexer = _expression.Substring(start, length);
int index = Convert.ToInt32(indexer, CultureInfo.InvariantCulture);
if (colonCount == 1)
{
endIndex = index;View on GitHub (pinned to 4f73e74372)
Solutions
- Remove the trailing comma so every slot has an integer index, e.g. $[1,2].
- When constructing index lists programmatically, filter out empty entries before joining with commas.
- Validate that each comma-separated segment is a non-negative integer.
Example fix
// before
token.SelectToken("$[1,]");
// after
token.SelectToken("$[1]"); Defensive patterns
Strategy: validation
Validate before calling
// Build a multi-index list, dropping empty entries
var indices = new[] { "1", "", "2" }.Where(s => !string.IsNullOrEmpty(s));
string path = $"$[{string.Join(",", indices)}]"; Prevention
- Filter empty values out of comma-separated index lists.
- Validate each index segment is a non-negative integer.
- Avoid trailing commas in multi-index expressions.
When it happens
Trigger: A path like token.SelectToken("$[1,]") or "$[,]" where a comma is followed by no digits before the closing ']'. The parser records the comma, then on the ']' finds an empty index segment.
Common situations: Building a multi-index list dynamically and leaving a trailing comma; truncating an index expression; copy/paste leaving an empty slot.
Related errors
- Unexpected character while parsing path indexer:
- 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/ea90aad629004d5d.
Report an issue: GitHub.