JamesNK/Newtonsoft.Json · error · JsonException

Unexpected character following indexer:

Error message

Unexpected character following indexer: 

What it means

Thrown by the JPath parser when a character appears immediately after a closed indexer (']' or ')') without the required '.' or '[' separator. The ParsePath default branch detects the followingIndexer flag and rejects the character, because two path segments must be separated by a dot or a new indexer bracket.

Source

Thrown at Src/Newtonsoft.Json/Linq/JsonPath/JPath.cs:169

                        {
                            scan = true;
                            _currentIndex++;
                        }
                        _currentIndex++;
                        currentPartStartIndex = _currentIndex;
                        followingIndexer = false;
                        followingDot = true;
                        break;
                    default:
                        if (query && (currentChar == '=' || currentChar == '<' || currentChar == '!' || currentChar == '>' || currentChar == '|' || currentChar == '&'))
                        {
                            ended = true;
                        }
                        else
                        {
                            if (followingIndexer)
                            {
                                throw new JsonException("Unexpected character following indexer: " + currentChar);
                            }

                            _currentIndex++;
                        }
                        break;
                }
            }

            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));

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Insert a '.' between the closing bracket and the next segment, e.g. $[0].foo.
  2. Double-check concatenated path segments include separators.
  3. Use the indexer form $[0]['foo'] if a dot is undesirable.

Example fix

// before
token.SelectToken("$.items[0]name");

// after
token.SelectToken("$.items[0].name");
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a separator between segments when building paths
static string JoinPath(params string[] segments) =>
    string.Join(".", segments.Where(s => !string.IsNullOrEmpty(s)));

Try / catch

try
{
    token.SelectToken(path);
}
catch (JsonException ex) when (ex.Message.StartsWith("Unexpected character following indexer"))
{
    // fix the path by inserting a '.' after the closing bracket
}

Prevention

When it happens

Trigger: A path like token.SelectToken("$[0]foo") or "$.a[0]b" where a field name directly follows ']' with no dot. The parser sets followingIndexer=true after the indexer and then hits the letter 'f'/'b' in the default branch (JPath.cs:167-169).

Common situations: Omitting the dot between an array index and the next property when hand-writing paths; string concatenation that drops a separator; converting array notation incorrectly.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/e96cd73a6a1ba989. Report an issue: GitHub.