JamesNK/Newtonsoft.Json · error · JsonException
Path ended with open indexer.
Error message
Path ended with open indexer.
What it means
Thrown by the JPath array-indexer parser when the expression ends while still inside an open indexer, i.e. the closing ']' was never reached. The parser loop exhausts the string at JPath.cs:384-385 and throws because the indexer is unbalanced. (The same message is also used by EnsureLength checks earlier when content runs out mid-indexer.)
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:385
start = _currentIndex;
end = null;
}
else if (!char.IsDigit(currentCharacter) && currentCharacter != '-')
{
throw new JsonException("Unexpected character while parsing path indexer: " + currentCharacter);
}
else
{
if (end != null)
{
throw new JsonException("Unexpected character while parsing path indexer: " + currentCharacter);
}
_currentIndex++;
}
}
throw new JsonException("Path ended with open indexer.");
}
private void EatWhitespace()
{
while (_currentIndex < _expression.Length)
{
if (_expression[_currentIndex] != ' ')
{
break;
}
_currentIndex++;
}
}
private PathFilter ParseQuery(char indexerCloseChar, bool scan)
{
_currentIndex++;View on GitHub (pinned to 4f73e74372)
Solutions
- Add the missing closing ']' to balance the indexer, e.g. $[1].
- When building paths dynamically, ensure every '[' has a matching ']'.
- Validate bracket balance before passing the path to SelectToken.
Example fix
// before
token.SelectToken("$.data[1");
// after
token.SelectToken("$.data[1]"); Defensive patterns
Strategy: validation
Validate before calling
// Verify bracket balance before querying
static bool BracketsBalanced(string path)
{
int depth = 0;
foreach (char c in path)
{
if (c == '[') depth++;
else if (c == ']') depth--;
if (depth < 0) return false;
}
return depth == 0;
} Try / catch
try
{
token.SelectToken(path);
}
catch (JsonException ex) when (ex.Message.Contains("Path ended with open indexer"))
{
path += "]"; // attempt to close the indexer
} Prevention
- Ensure every '[' has a matching ']' in the path.
- Use a path-builder that tracks and closes open indexers.
- Validate bracket balance before calling SelectToken.
When it happens
Trigger: A path like token.SelectToken("$[1") or "$[" where the opening '[' has no matching ']'. The parser enters ParseArrayIndexer and runs off the end of the string.
Common situations: Truncating a path string; building a path with string concatenation that omits the closing bracket; a formatting bug that drops trailing characters.
Related errors
- Unexpected character while parsing path:
- Unexpected character following indexer:
- Unexpected end while parsing path.
- 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/c3aa646c4423cba9.
Report an issue: GitHub.