JamesNK/Newtonsoft.Json · error · Exception
Cannot deserialize non-cubical array as multidimensional arr
Error message
Cannot deserialize non-cubical array as multidimensional array.
What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Utilities/CollectionUtils.cs:331
return dimensions;
}
private static void CopyFromJaggedToMultidimensionalArray(IList values, Array multidimensionalArray, int[] indices)
{
int dimension = indices.Length;
if (dimension == multidimensionalArray.Rank)
{
multidimensionalArray.SetValue(JaggedArrayGetValue(values, indices), indices);
return;
}
int dimensionLength = multidimensionalArray.GetLength(dimension);
IList list = (IList)JaggedArrayGetValue(values, indices);
int currentValuesLength = list.Count;
if (currentValuesLength != dimensionLength)
{
throw new Exception("Cannot deserialize non-cubical array as multidimensional array.");
}
int[] newIndices = new int[dimension + 1];
for (int i = 0; i < dimension; i++)
{
newIndices[i] = indices[i];
}
for (int i = 0; i < multidimensionalArray.GetLength(dimension); i++)
{
newIndices[dimension] = i;
CopyFromJaggedToMultidimensionalArray(values, multidimensionalArray, newIndices);
}
}
private static object JaggedArrayGetValue(IList values, int[] indices)
{
IList currentList = values;View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before: non-cubical JSON into int[,]
int[,] grid = JsonConvert.DeserializeObject<int[,]>("[[1,2,3],[4,5]]");
// after: deserialize jagged then convert
int[][] jagged = JsonConvert.DeserializeObject<int[][]>("[[1,2,3],[4,5,0]]");
int[,] grid = ToRectangular(jagged); Defensive patterns
Strategy: validation
Validate before calling
bool IsCubical<T>(T[][] jagged) => jagged.Length == 0 || jagged.All(r => r.Length == jagged[0].Length);
Type guard
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; } Try / catch
try { var arr = JsonConvert.DeserializeObject<int[,]>(json); }
catch (Exception ex) when (ex.Message.Contains("non-cubical array")) {
logger.Error(ex, "rows have unequal length; deserialize as jagged or normalize JSON."); throw;
} Prevention
- 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.
When it happens
Trigger: 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]].
Common situations: 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.
Related errors
- Unexpected BsonType value: {0}
- Unable to read beyond the end of the stream.
- Expected Bytes but got {0}.
- No object created.
- Expected JSON property '{0}'.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/faab217ed02727c9.
Report an issue: GitHub.