{"record":{"id":"670c7e81bc2656d3","repo":"dotnet/efcore","slug":"the-number-of-indices-provided-indicescount-mu","errorCode":null,"errorMessage":"The number of indices provided ({indicesCount}) must match the number of array segments in the JSON path ({arraySegmentCount}).","messagePattern":"The number of indices provided \\((.+?)\\) must match the number of array segments in the JSON path \\((.+?)\\)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/StructuredJsonPath.cs","lineNumber":39,"sourceCode":"\n    /// <summary>\n    ///     Creates a new <see cref=\"StructuredJsonPath\" /> instance.\n    /// </summary>\n    /// <param name=\"segments\">The path segments.</param>\n    /// <param name=\"indices\">\n    ///     The index values for array index placeholders. Must have one entry for each segment\n    ///     where <see cref=\"StructuredJsonPathSegment.IsArray\" /> is <see langword=\"true\" />. A\n    ///     <see langword=\"null\" /> entry means the indexer is unspecified (all elements) and is\n    ///     rendered as <c>[*]</c> by default, or as <c>[]</c>.\n    /// </param>\n    public StructuredJsonPath(IReadOnlyList<StructuredJsonPathSegment> segments, IReadOnlyList<int?>? indices)\n    {\n        var arraySegmentCount = segments.Count(s => s.IsArray);\n        if (indices is null\n                ? arraySegmentCount != 0\n                : indices.Count != arraySegmentCount)\n        {\n            throw new ArgumentException(\n                CoreStrings.InvalidStructuredJsonPathIndexCount(indices?.Count ?? 0, arraySegmentCount),\n                nameof(indices));\n        }\n\n        Segments = segments;\n        Indices = indices;\n    }\n\n    /// <summary>\n    ///     Gets the path segments.\n    /// </summary>\n    public virtual IReadOnlyList<StructuredJsonPathSegment> Segments { get; }\n\n    /// <summary>\n    ///     Gets the index values for array index placeholders. The indices are applied in order\n    ///     to the segments where <see cref=\"StructuredJsonPathSegment.IsArray\" /> is <see langword=\"true\" />.\n    ///     A <see langword=\"null\" /> entry means the indexer is unspecified (all elements).\n    /// </summary>","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/StructuredJsonPath.cs#L21-L57","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Count array segments first and supply exactly that many index entries: indices = arraySegments.Select(...).ToList().","Pass null per slot (List<int?>) to express 'all elements' ([*]) for an array position rather than omitting it.","Validate or assert the counts match before constructing; surface a clearer error to callers."],"exampleFix":"// before\nvar segments = new[] { new StructuredJsonPathSegment(\"items\", isArray: true), new StructuredJsonPathSegment(\"sku\") };\nvar path = new StructuredJsonPath(segments, new int?[] { }); // 0 indices, 1 array segment -> [452]\n\n// after\nvar path = new StructuredJsonPath(segments, new int?[] { 0 }); // one index for 'items[0].sku'","handlingStrategy":"validation","validationCode":"// Assert indices length matches array-segment count before constructing.\nstatic bool IndicesMatchSegments(IReadOnlyList<StructuredJsonPathSegment> segments, IReadOnlyList<int?>? indices)\n{\n    var arrayCount = segments.Count(s => s.IsArray);\n    return indices is null ? arrayCount == 0 : indices.Count == arrayCount;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute indices from the array segments themselves to guarantee the count matches.","Use null entries (List<int?>) for 'all elements' rather than omitting slots.","Wrap StructuredJsonPath construction in a helper that validates counts."],"tags":["json","arguments","structured-json-path"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}