JamesNK/Newtonsoft.Json · error · JsonException
Index {0} outside the bounds of JConstructor.
Error message
Index {0} outside the bounds of JConstructor. What it means
Thrown by PathFilter.GetTokenIndex when an index applied to a JConstructor token is greater than or equal to the constructor's Count, but only when JsonSelectSettings.ErrorWhenNoMatch is true. JConstructor is the JSON construct for non-object/array container types (e.g. constructor syntax like new Date(...)).
Source
Thrown at Src/Newtonsoft.Json/Linq/JsonPath/PathFilter.cs:33
if (a.Count <= index)
{
if (settings?.ErrorWhenNoMatch ?? false)
{
throw new JsonException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, index));
}
return null;
}
return a[index];
}
else if (t is JConstructor c)
{
if (c.Count <= index)
{
if (settings?.ErrorWhenNoMatch ?? false)
{
throw new JsonException("Index {0} outside the bounds of JConstructor.".FormatWith(CultureInfo.InvariantCulture, index));
}
return null;
}
return c[index];
}
else
{
if (settings?.ErrorWhenNoMatch ?? false)
{
throw new JsonException("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, index, t.GetType().Name));
}
return null;
}
}
View on GitHub (pinned to 4f73e74372)
Solutions
- Verify the token type (token.Type == JTokenType.Constructor) and its Count before indexing.
- Drop ErrorWhenNoMatch to make out-of-range indices return null silently.
- Adjust the index to a valid position or use property/scan filters instead.
Example fix
// before
token.SelectTokens("$[2]", new JsonSelectSettings { ErrorWhenNoMatch = true });
// after
var ctor = (JConstructor)token;
if (ctor.Count > 2)
{
token.SelectTokens("$[2]", new JsonSelectSettings { ErrorWhenNoMatch = true });
} Defensive patterns
Strategy: type-guard
Validate before calling
static bool CanIndex(JToken token, int index)
{
return token is JConstructor c && index >= 0 && index < c.Count;
} Type guard
bool IsIndexableConstructor(JToken t) => t is JConstructor c && c.Count > 0;
Try / catch
try { token.SelectTokens(path, settings).ToList(); }
catch (JsonException ex) when (ex.Message.Contains("outside the bounds of JConstructor"))
{ /* constructor argument index out of range */ } Prevention
- Confirm token.Type == JTokenType.Constructor before positional indexing.
- Check JConstructor.Count before indexing.
When it happens
Trigger: Running an index filter over a JConstructor token with ErrorWhenNoMatch=true where the index is out of range. Example: token.SelectTokens("$[1]", new JsonSelectSettings { ErrorWhenNoMatch = true }) when token is a JConstructor with a single argument.
Common situations: Parsing JSON that uses constructor syntax and then running a positional index on it; payloads whose constructor arity changed; assuming the token is an array when it is actually a constructor.
Related errors
- Index {0} outside the bounds of JArray.
- Path ended with open query.
- Unknown escape character: \
- Path ended with an open string.
- Path ended with an open regex.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/be9e1ff83df16579.
Report an issue: GitHub.