JamesNK/Newtonsoft.Json · error · JsonException

Array slice of {0} to {1} returned no results.

Error message

Array slice of {0} to {1} returned no results.

What it means

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).

Source

Thrown at Src/Newtonsoft.Json/Linq/JsonPath/ArraySliceFilter.cs:61

                    startIndex = Math.Max(startIndex, (stepCount > 0) ? 0 : int.MinValue);
                    startIndex = Math.Min(startIndex, (stepCount > 0) ? a.Count : a.Count - 1);
                    stopIndex = Math.Max(stopIndex, -1);
                    stopIndex = Math.Min(stopIndex, a.Count);

                    bool positiveStep = (stepCount > 0);

                    if (IsValid(startIndex, stopIndex, positiveStep))
                    {
                        for (int i = startIndex; IsValid(i, stopIndex, positiveStep); i += stepCount)
                        {
                            yield return a[i];
                        }
                    }
                    else
                    {
                        if (settings?.ErrorWhenNoMatch ?? false)
                        {
                            throw new JsonException("Array slice of {0} to {1} returned no results.".FormatWith(CultureInfo.InvariantCulture,
                                Start != null ? Start.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*",
                                End != null ? End.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*"));
                        }
                    }
                }
                else
                {
                    if (settings?.ErrorWhenNoMatch ?? false)
                    {
                        throw new JsonException("Array slice is not valid on {0}.".FormatWith(CultureInfo.InvariantCulture, t.GetType().Name));
                    }
                }
            }
        }

        private bool IsValid(int index, int stopIndex, bool positiveStep)
        {
            if (positiveStep)

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Call SelectTokens without errorWhenNoMatch so an empty slice silently returns zero results.
  2. Clamp slice start/end to the array's actual bounds before building the path expression.
  3. Check token["data"]?.Count() or the array length and skip the slice when the range is out of bounds.

Example fix

// before
token.SelectTokens("$.data[5:10]", errorWhenNoMatch: true);

// after
token.SelectTokens("$.data[5:10]", errorWhenNoMatch: false); // empty slice => no results, no throw
Defensive patterns

Strategy: validation

Validate before calling

// Clamp slice bounds to the actual array length
var arr = token["data"] as JArray;
if (arr != null)
{
    int max = Math.Min(5, arr.Count);
    for (int i = 0; i < max; i++) { /* use arr[i] */ }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/f2c7948add38727e. Report an issue: GitHub.