{"record":{"id":"19c4114288c295e4","repo":"JamesNK/Newtonsoft.Json","slug":"index-0-outside-the-bounds-of-jarray","errorCode":null,"errorMessage":"Index {0} outside the bounds of JArray.","messagePattern":"Index (.+?) outside the bounds of JArray\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JsonPath/PathFilter.cs","lineNumber":19,"sourceCode":"using System.Collections.Generic;\nusing System.Globalization;\nusing Newtonsoft.Json.Utilities;\n\nnamespace Newtonsoft.Json.Linq.JsonPath\n{\n    internal abstract class PathFilter\n    {\n        public abstract IEnumerable<JToken> ExecuteFilter(JToken root, IEnumerable<JToken> current, JsonSelectSettings? settings);\n\n        protected static JToken? GetTokenIndex(JToken t, JsonSelectSettings? settings, int index)\n        {\n            if (t is JArray a)\n            {\n                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                }","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JsonPath/PathFilter.cs#L1-L37","documentation":"Thrown by PathFilter.GetTokenIndex when an array index applied to a JArray is greater than or equal to the array's Count, but only when JsonSelectSettings.ErrorWhenNoMatch is true. Without that flag the filter silently returns null (no match).","triggerScenarios":"Calling SelectTokens with ErrorWhenNoMatch=true and an index that exceeds the array size. Example: token.SelectTokens(\"$.items[10]\", new JsonSelectSettings { ErrorWhenNoMatch = true }) on an array with fewer than 11 elements.","commonSituations":"Hard-coded indices assuming a minimum payload size; payloads that changed shape between environments; negative-or-zero-length arrays from an upstream service outage; enabling ErrorWhenNoMatch to get strict behavior and then hitting variable-length data.","solutions":["Check the array length before indexing, or omit ErrorWhenNoMatch so out-of-range indices yield no token instead of throwing.","Use array slice or scan filters ($..) instead of absolute indices when length is variable.","Validate payload shape (e.g. minimum array length) before running the path."],"exampleFix":"// before\ntoken.SelectTokens(\"$.items[10]\", new JsonSelectSettings { ErrorWhenNoMatch = true });\n// after\nvar arr = (JArray)token[\"items\"];\nif (arr.Count > 10)\n{\n    token.SelectTokens(\"$.items[10]\", new JsonSelectSettings { ErrorWhenNoMatch = true });\n}","handlingStrategy":"validation","validationCode":"static bool CanIndex(JToken token, int index)\n{\n    return token is JArray a && index >= 0 && index < a.Count;\n}","typeGuard":"bool IsIndexableArray(JToken t) => t is JArray && t.HasValues;","tryCatchPattern":"try { token.SelectTokens(\"$.items[10]\", settings).ToList(); }\ncatch (JsonException ex) when (ex.Message.Contains(\"outside the bounds of JArray\"))\n{ /* index too large; degrade gracefully */ }","preventionTips":["Check JArray.Count before indexing.","Drop ErrorWhenNoMatch when array length is not guaranteed.","Use slices or scans for variable-length data."],"tags":["jsonpath","json","jarray","bounds","jpath"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}