{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/StructuredJsonPath.cs#L21-L57","documentation":"Thrown by the StructuredJsonPath constructor (StructuredJsonPath.cs:32-42) as an ArgumentException when the number of provided index values does not match the number of array segments in the path. For every path segment where IsArray is true, exactly one int? index entry must be supplied (null means 'all elements'). The constructor counts array segments and compares to indices.Count.","triggerScenarios":"Constructing a new StructuredJsonPath(segments, indices) where segments contains N array-type segments but indices has a different count. Passing null indices when segments contain array segments, or passing too few/too many indices.","commonSituations":"Building a JSON path programmatically and mismatching the array-index list length. Passing indices for non-array segments or omitting indices for array segments. Low-level API misuse when constructing structured JSON paths for queries or updates.","solutions":["Count the array segments (those with IsArray == true) in your segments list and provide exactly that many index entries.","If the path has no array segments, pass null (or an empty list) for indices.","Use a helper: var indices = segments.Where(s => s.IsArray).Select(s => (int?)0).ToList();"],"exampleFix":"// before: 2 array segments but only 1 index\nvar path = new StructuredJsonPath(\n    new[] { segProperty, segArray1, segArray2 },\n    new int?[] { 0 }); // throws\n\n// after: one index per array segment\nvar path = new StructuredJsonPath(\n    new[] { segProperty, segArray1, segArray2 },\n    new int?[] { 0, null }); // 0 for first array, null (all) for second","handlingStrategy":"validation","validationCode":"int arraySegCount = segments.Count(s => s.IsArray);\nif (indices == null && arraySegCount != 0)\n    throw new ArgumentException($\"Path has {arraySegCount} array segments but indices is null.\");\nif (indices != null && indices.Count != arraySegCount)\n    throw new ArgumentException($\"Expected {arraySegCount} indices, got {indices.Count}.\");\nvar path = new StructuredJsonPath(segments, indices);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always compute the indices list from the segments: segments.Where(s => s.IsArray).Select(_ => (int?)0).ToArray().","If your path has no array segments, pass null for indices.","Unit-test StructuredJsonPath construction with varying array-segment counts."],"tags":["json-path","argument-validation","structured-json"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}