JamesNK/Newtonsoft.Json · error · JsonException

Array slice is not valid on {0}.

Error message

Array slice is not valid on {0}.

What it means

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.

Source

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

                        {
                            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)
            {
                return (index < stopIndex);
            }

            return (index > stopIndex);
        }
    }
}

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Confirm the token at the slice target is a JArray before using slice syntax.
  2. Omit errorWhenNoMatch so a non-array target produces zero results instead of an exception.
  3. Normalize the source JSON so the field is consistently an array.

Example fix

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

// after
var data = token["data"];
var results = (data is JArray)
    ? data.SelectTokens("[0:5]")
    : Enumerable.Empty<JToken>();
Defensive patterns

Strategy: type-guard

Validate before calling

var node = token["data"];
var results = (node is JArray)
    ? token.SelectTokens("$.data[0:5]")
    : Enumerable.Empty<JToken>();

Type guard

static bool IsJsonArray(JToken? t) => t is JArray;

Prevention

When it happens

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

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

Related errors


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