{"record":{"id":"1cf516bbb72fa789","repo":"JamesNK/Newtonsoft.Json","slug":"array-slice-is-not-valid-on-0","errorCode":null,"errorMessage":"Array slice is not valid on {0}.","messagePattern":"Array slice is not valid on (.+?)\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JsonPath/ArraySliceFilter.cs","lineNumber":71,"sourceCode":"                        {\n                            yield return a[i];\n                        }\n                    }\n                    else\n                    {\n                        if (settings?.ErrorWhenNoMatch ?? false)\n                        {\n                            throw new JsonException(\"Array slice of {0} to {1} returned no results.\".FormatWith(CultureInfo.InvariantCulture,\n                                Start != null ? Start.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : \"*\",\n                                End != null ? End.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : \"*\"));\n                        }\n                    }\n                }\n                else\n                {\n                    if (settings?.ErrorWhenNoMatch ?? false)\n                    {\n                        throw new JsonException(\"Array slice is not valid on {0}.\".FormatWith(CultureInfo.InvariantCulture, t.GetType().Name));\n                    }\n                }\n            }\n        }\n\n        private bool IsValid(int index, int stopIndex, bool positiveStep)\n        {\n            if (positiveStep)\n            {\n                return (index < stopIndex);\n            }\n\n            return (index > stopIndex);\n        }\n    }\n}","sourceCodeStart":53,"sourceCodeEnd":87,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JsonPath/ArraySliceFilter.cs#L53-L87","documentation":"Thrown during JSONPath evaluation when an array slice expression ([start:end:step] or [:]) is applied to a token that is not a JArray, and ErrorWhenNoMatch is true. The ArraySliceFilter only knows how to slice arrays; any other node type (JObject, JValue, JProperty) is rejected.","triggerScenarios":"Calling SelectTokens(\"$.data[0:5]\", errorWhenNoMatch: true) where 'data' resolves to a JObject or scalar JValue rather than a JArray. The non-array branch (ArraySliceFilter.cs:67-72) reports the actual type name.","commonSituations":"Schema drift where an array field became a single object; polymorphic responses that return an object for one item but an array for many; assuming a list shape for a nullable scalar field.","solutions":["Confirm the token at the slice target is a JArray before using slice syntax.","Omit errorWhenNoMatch so a non-array target produces zero results instead of an exception.","Normalize the source JSON so the field is consistently an array."],"exampleFix":"// before\ntoken.SelectTokens(\"$.data[0:5]\", errorWhenNoMatch: true);\n\n// after\nvar data = token[\"data\"];\nvar results = (data is JArray)\n    ? data.SelectTokens(\"[0:5]\")\n    : Enumerable.Empty<JToken>();","handlingStrategy":"type-guard","validationCode":"var node = token[\"data\"];\nvar results = (node is JArray)\n    ? token.SelectTokens(\"$.data[0:5]\")\n    : Enumerable.Empty<JToken>();","typeGuard":"static bool IsJsonArray(JToken? t) => t is JArray;","tryCatchPattern":null,"preventionTips":["Verify the target node is a JArray before using slice syntax.","Keep errorWhenNoMatch off unless you explicitly need strict failures.","Document the expected shape of each path segment in your query layer."],"tags":["jsonpath","type-mismatch","array-slice"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}