{"record":{"id":"faab217ed02727c9","repo":"JamesNK/Newtonsoft.Json","slug":"cannot-deserialize-non-cubical-array-as-multidimen","errorCode":null,"errorMessage":"Cannot deserialize non-cubical array as multidimensional array.","messagePattern":"Cannot deserialize non-cubical array as multidimensional array\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/CollectionUtils.cs","lineNumber":331,"sourceCode":"\n            return dimensions;\n        }\n\n        private static void CopyFromJaggedToMultidimensionalArray(IList values, Array multidimensionalArray, int[] indices)\n        {\n            int dimension = indices.Length;\n            if (dimension == multidimensionalArray.Rank)\n            {\n                multidimensionalArray.SetValue(JaggedArrayGetValue(values, indices), indices);\n                return;\n            }\n\n            int dimensionLength = multidimensionalArray.GetLength(dimension);\n            IList list = (IList)JaggedArrayGetValue(values, indices);\n            int currentValuesLength = list.Count;\n            if (currentValuesLength != dimensionLength)\n            {\n                throw new Exception(\"Cannot deserialize non-cubical array as multidimensional array.\");\n            }\n\n            int[] newIndices = new int[dimension + 1];\n            for (int i = 0; i < dimension; i++)\n            {\n                newIndices[i] = indices[i];\n            }\n\n            for (int i = 0; i < multidimensionalArray.GetLength(dimension); i++)\n            {\n                newIndices[dimension] = i;\n                CopyFromJaggedToMultidimensionalArray(values, multidimensionalArray, newIndices);\n            }\n        }\n\n        private static object JaggedArrayGetValue(IList values, int[] indices)\n        {\n            IList currentList = values;","sourceCodeStart":313,"sourceCodeEnd":349,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/CollectionUtils.cs#L313-L349","documentation":"Thrown by CollectionUtils.CopyFromJaggedToMultidimensionalArray when the jagged-array representation of a deserialized multidimensional array has rows of unequal length (non-cubical). A multidimensional array (e.g. int[2,3]) is rectangular; if the JSON encodes rows with different lengths the resulting jagged structure is not cubical and cannot be copied into a rectangular array.","triggerScenarios":"Deserializing JSON into a multidimensional array (e.g. int[,] target) where the JSON arrays per row have differing counts, e.g. [[1,2,3],[4,5]].","commonSituations":"Source JSON produced by a jagged-array producer rather than a rectangular one; sparse matrix data; mismatched schema where the producer assumes jagged but the consumer declares a multidimensional array; malformed/partial payload from an external API.","solutions":["Validate/normalize the JSON so every row array has the same length before deserializing.","Deserialize into a jagged array (T[][]) instead of a rectangular multidimensional array (T[,]).","Use a custom JsonConverter that pads or rejects non-rectangular input explicitly.","Fix the producer to always emit rectangular data."],"exampleFix":"// before: non-cubical JSON into int[,]\nint[,] grid = JsonConvert.DeserializeObject<int[,]>(\"[[1,2,3],[4,5]]\");\n// after: deserialize jagged then convert\nint[][] jagged = JsonConvert.DeserializeObject<int[][]>(\"[[1,2,3],[4,5,0]]\");\nint[,] grid = ToRectangular(jagged);","handlingStrategy":"validation","validationCode":"bool IsCubical<T>(T[][] jagged) => jagged.Length == 0 || jagged.All(r => r.Length == jagged[0].Length);","typeGuard":"static bool IsCubical(Array jagged) { if (jagged.Length == 0) return true; int len = ((Array)jagged.GetValue(0)).Length; for (int i=1;i<jagged.Length;i++) if (((Array)jagged.GetValue(i)).Length != len) return false; return true; }","tryCatchPattern":"try { var arr = JsonConvert.DeserializeObject<int[,]>(json); }\ncatch (Exception ex) when (ex.Message.Contains(\"non-cubical array\")) {\n    logger.Error(ex, \"rows have unequal length; deserialize as jagged or normalize JSON.\"); throw;\n}","preventionTips":["Validate row lengths are uniform before deserializing into a multidimensional array.","Prefer T[][] (jagged) target types for variable-length row data.","Use a custom JsonConverter to enforce rectangular shape with a clear error.","Ensure producers always emit rectangular data."],"tags":["deserialization","multidimensional-array","array","collection-utils"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}