{"record":{"id":"f2c7948add38727e","repo":"JamesNK/Newtonsoft.Json","slug":"array-slice-of-0-to-1-returned-no-results","errorCode":null,"errorMessage":"Array slice of {0} to {1} returned no results.","messagePattern":"Array slice of (.+?) to (.+?) returned no results\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JsonPath/ArraySliceFilter.cs","lineNumber":61,"sourceCode":"                    startIndex = Math.Max(startIndex, (stepCount > 0) ? 0 : int.MinValue);\n                    startIndex = Math.Min(startIndex, (stepCount > 0) ? a.Count : a.Count - 1);\n                    stopIndex = Math.Max(stopIndex, -1);\n                    stopIndex = Math.Min(stopIndex, a.Count);\n\n                    bool positiveStep = (stepCount > 0);\n\n                    if (IsValid(startIndex, stopIndex, positiveStep))\n                    {\n                        for (int i = startIndex; IsValid(i, stopIndex, positiveStep); i += stepCount)\n                        {\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)","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JsonPath/ArraySliceFilter.cs#L43-L79","documentation":"Thrown during JSONPath evaluation when an array slice is applied to a JArray but the resolved start/stop/step range yields no elements, and ErrorWhenNoMatch is true. The filter computed a valid but empty range (e.g. start beyond the array length, or a negative step where start is already below stop).","triggerScenarios":"Calling SelectTokens(\"$.data[5:10]\", errorWhenNoMatch: true) on an array with fewer than 5 elements, or $.data[0:-100] with a large negative end, so IsValid() returns false and the slice produces nothing. The message reports the Start and End boundaries requested.","commonSituations":"Pagination indices that exceed the actual array length; hard-coded slice offsets that assume a minimum data size; negative indices computed against unexpectedly small arrays.","solutions":["Call SelectTokens without errorWhenNoMatch so an empty slice silently returns zero results.","Clamp slice start/end to the array's actual bounds before building the path expression.","Check token[\"data\"]?.Count() or the array length and skip the slice when the range is out of bounds."],"exampleFix":"// before\ntoken.SelectTokens(\"$.data[5:10]\", errorWhenNoMatch: true);\n\n// after\ntoken.SelectTokens(\"$.data[5:10]\", errorWhenNoMatch: false); // empty slice => no results, no throw","handlingStrategy":"validation","validationCode":"// Clamp slice bounds to the actual array length\nvar arr = token[\"data\"] as JArray;\nif (arr != null)\n{\n    int max = Math.Min(5, arr.Count);\n    for (int i = 0; i < max; i++) { /* use arr[i] */ }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use errorWhenNoMatch=false for slice queries so empty ranges return nothing instead of throwing.","Check the array length and clamp slice offsets before building the path.","Treat an empty slice result as normal pagination edge-case, not an error."],"tags":["jsonpath","array-slice","out-of-range"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}