dotnet/efcore · error · ArgumentException

The number of indices provided

Error message

The number of indices provided ({indicesCount}) must match the number of array segments in the JSON path ({arraySegmentCount}).

What it means

StructuredJsonPath models a JSON path with placeholder indexers for array segments. Its constructor requires the indices list to contain exactly one entry per array segment (segments.Count(s => s.IsArray)). null indices is only valid when there are zero array segments. A mismatched count is an ArgumentException (invalid argument, not a model error).

Solutions

  1. Count array segments first and supply exactly that many index entries: indices = arraySegments.Select(...).ToList().
  2. Pass null per slot (List<int?>) to express 'all elements' ([*]) for an array position rather than omitting it.
  3. Validate or assert the counts match before constructing; surface a clearer error to callers.

Example fix

// before
var segments = new[] { new StructuredJsonPathSegment("items", isArray: true), new StructuredJsonPathSegment("sku") };
var path = new StructuredJsonPath(segments, new int?[] { }); // 0 indices, 1 array segment -> [452]

// after
var path = new StructuredJsonPath(segments, new int?[] { 0 }); // one index for 'items[0].sku'
Defensive patterns

Strategy: validation

Validate before calling

// Assert indices length matches array-segment count before constructing.
static bool IndicesMatchSegments(IReadOnlyList<StructuredJsonPathSegment> segments, IReadOnlyList<int?>? indices)
{
    var arrayCount = segments.Count(s => s.IsArray);
    return indices is null ? arrayCount == 0 : indices.Count == arrayCount;
}

Prevention

When it happens

Trigger: Constructing new StructuredJsonPath(segments, indices) where indices.Count != number of array segments; passing null indices when at least one segment IsArray; passing too few/too many index placeholders for [*]/[] slots.

Common situations: Programmatic JSON-path construction; building dynamic paths for collection indexers; off-by-one when computing index positions; library code that threads user input into indices.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/670c7e81bc2656d3. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Relational/Infrastructure/StructuredJsonPath.cs:39

    /// <summary>
    ///     Creates a new <see cref="StructuredJsonPath" /> instance.
    /// </summary>
    /// <param name="segments">The path segments.</param>
    /// <param name="indices">
    ///     The index values for array index placeholders. Must have one entry for each segment
    ///     where <see cref="StructuredJsonPathSegment.IsArray" /> is <see langword="true" />. A
    ///     <see langword="null" /> entry means the indexer is unspecified (all elements) and is
    ///     rendered as <c>[*]</c> by default, or as <c>[]</c>.
    /// </param>
    public StructuredJsonPath(IReadOnlyList<StructuredJsonPathSegment> segments, IReadOnlyList<int?>? indices)
    {
        var arraySegmentCount = segments.Count(s => s.IsArray);
        if (indices is null
                ? arraySegmentCount != 0
                : indices.Count != arraySegmentCount)
        {
            throw new ArgumentException(
                CoreStrings.InvalidStructuredJsonPathIndexCount(indices?.Count ?? 0, arraySegmentCount),
                nameof(indices));
        }

        Segments = segments;
        Indices = indices;
    }

    /// <summary>
    ///     Gets the path segments.
    /// </summary>
    public virtual IReadOnlyList<StructuredJsonPathSegment> Segments { get; }

    /// <summary>
    ///     Gets the index values for array index placeholders. The indices are applied in order
    ///     to the segments where <see cref="StructuredJsonPathSegment.IsArray" /> is <see langword="true" />.
    ///     A <see langword="null" /> entry means the indexer is unspecified (all elements).
    /// </summary>

View on GitHub (pinned to 3a2006ef56)