JamesNK/Newtonsoft.Json · error · ArgumentException
The number of elements in the source JObject is greater than
Error message
The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.
What it means
ArgumentException thrown by JObject's ICollection<KeyValuePair<string,JToken?>>.CopyTo when the destination array does not have enough space from arrayIndex to its end to hold all the JObject's properties. The count of source properties exceeds (array.Length - arrayIndex).
Source
Thrown at Src/Newtonsoft.Json/Linq/JObject.cs:683
}
void ICollection<KeyValuePair<string, JToken?>>.CopyTo(KeyValuePair<string, JToken?>[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "arrayIndex is less than 0.");
}
if (arrayIndex >= array.Length && arrayIndex != 0)
{
throw new ArgumentException("arrayIndex is equal to or greater than the length of array.");
}
if (Count > array.Length - arrayIndex)
{
throw new ArgumentException("The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array.");
}
int index = 0;
foreach (JProperty property in _properties)
{
array[arrayIndex + index] = new KeyValuePair<string, JToken?>(property.Name, property.Value);
index++;
}
}
bool ICollection<KeyValuePair<string, JToken?>>.IsReadOnly => false;
bool ICollection<KeyValuePair<string, JToken?>>.Remove(KeyValuePair<string, JToken?> item)
{
if (!((ICollection<KeyValuePair<string, JToken?>>)this).Contains(item))
{
return false;
}View on GitHub (pinned to 4f73e74372)
Solutions
- Size the destination from the actual source count: new KeyValuePair<string,JToken?>[jObject.Count + arrayIndex].
- Use LINQ ToArray()/ToList() which allocate correctly for you.
- Track a running offset and resize the buffer when remaining space is insufficient.
Example fix
// before var arr = new KeyValuePair<string, JToken?>[2]; ((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, 0); // too small // after var arr = new KeyValuePair<string, JToken?>[jObject.Count]; ((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, 0);
Defensive patterns
Strategy: validation
Validate before calling
if (jObject.Count > array.Length - arrayIndex) throw new ArgumentException("Destination too small.");
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(array, arrayIndex); Try / catch
try { ((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, idx); }
catch (ArgumentException ex) when (ex.Message.Contains("greater than the available space"))
{
Array.Resize(ref arr, idx + jObject.Count);
((ICollection<KeyValuePair<string,JToken?>>)jObject).CopyTo(arr, idx);
} Prevention
- Size the destination from the actual source: jObject.Count + arrayIndex.
- Use LINQ ToArray()/ToList() to avoid manual sizing.
- Resize buffers when remaining space is insufficient.
When it happens
Trigger: Calling CopyTo with a destination array sized smaller than jObject.Count minus arrayIndex — e.g. a 2-element buffer for a 5-property object, or a valid arrayIndex that leaves too little room.
Common situations: Reusing a fixed-size buffer; sizing the destination from an estimated count that is too small; chaining CopyTo from multiple sources into one buffer without accounting for prior writes.
Related errors
- arrayIndex is less than 0.
- arrayIndex is equal to or greater than the length of array.
- array
- Unexpected merge array handling when merging JSON.
- Can not add {0} to {1}.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/ddfccb5b8884a220.
Report an issue: GitHub.