{"record":{"id":"be9e1ff83df16579","repo":"JamesNK/Newtonsoft.Json","slug":"index-0-outside-the-bounds-of-jconstructor","errorCode":null,"errorMessage":"Index {0} outside the bounds of JConstructor.","messagePattern":"Index (.+?) outside the bounds of JConstructor\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JsonPath/PathFilter.cs","lineNumber":33,"sourceCode":"                if (a.Count <= index)\n                {\n                    if (settings?.ErrorWhenNoMatch ?? false)\n                    {\n                        throw new JsonException(\"Index {0} outside the bounds of JArray.\".FormatWith(CultureInfo.InvariantCulture, index));\n                    }\n\n                    return null;\n                }\n\n                return a[index];\n            }\n            else if (t is JConstructor c)\n            {\n                if (c.Count <= index)\n                {\n                    if (settings?.ErrorWhenNoMatch ?? false)\n                    {\n                        throw new JsonException(\"Index {0} outside the bounds of JConstructor.\".FormatWith(CultureInfo.InvariantCulture, index));\n                    }\n\n                    return null;\n                }\n\n                return c[index];\n            }\n            else\n            {\n                if (settings?.ErrorWhenNoMatch ?? false)\n                {\n                    throw new JsonException(\"Index {0} not valid on {1}.\".FormatWith(CultureInfo.InvariantCulture, index, t.GetType().Name));\n                }\n\n                return null;\n            }\n        }\n","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JsonPath/PathFilter.cs#L15-L51","documentation":"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(...)).","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\ntoken.SelectTokens(\"$[2]\", new JsonSelectSettings { ErrorWhenNoMatch = true });\n// after\nvar ctor = (JConstructor)token;\nif (ctor.Count > 2)\n{\n    token.SelectTokens(\"$[2]\", new JsonSelectSettings { ErrorWhenNoMatch = true });\n}","handlingStrategy":"type-guard","validationCode":"static bool CanIndex(JToken token, int index)\n{\n    return token is JConstructor c && index >= 0 && index < c.Count;\n}","typeGuard":"bool IsIndexableConstructor(JToken t) => t is JConstructor c && c.Count > 0;","tryCatchPattern":"try { token.SelectTokens(path, settings).ToList(); }\ncatch (JsonException ex) when (ex.Message.Contains(\"outside the bounds of JConstructor\"))\n{ /* constructor argument index out of range */ }","preventionTips":["Confirm token.Type == JTokenType.Constructor before positional indexing.","Check JConstructor.Count before indexing."],"tags":["jsonpath","json","jconstructor","bounds","jpath"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}