JamesNK/Newtonsoft.Json · error · ArgumentException

arrayIndex is equal to or greater than the length of array.

Error message

arrayIndex is equal to or greater than the length of array.

What it means

ArgumentException thrown by JObject's ICollection<KeyValuePair<string,JToken?>>.CopyTo when arrayIndex is >= the destination array length (and not 0). The starting position must fall within the array; an out-of-range start is rejected.

Source

Thrown at Src/Newtonsoft.Json/Linq/JObject.cs:679

                return false;
            }

            return (property.Value == item.Value);
        }

        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)
        {

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Ensure 0 <= arrayIndex < array.Length before calling (arrayIndex 0 is permitted even when the array is empty).
  2. Allocate a destination large enough and start at 0, or track the running index and assert it stays in range.
  3. Switch to LINQ ToArray() if buffer arithmetic is error-prone.

Example fix

// before
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, arr.Length); // out of range

// after
if (arrayIndex < 0 || arrayIndex > arr.Length) throw new ArgumentException("Bad index");
((ICollection<KeyValuePair<string, JToken?>>)jObject).CopyTo(arr, arrayIndex);
Defensive patterns

Strategy: validation

Validate before calling

if (arrayIndex > array.Length) throw new ArgumentException("arrayIndex beyond array length.");
((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 length")) { /* reset idx */ }

Prevention

When it happens

Trigger: Calling CopyTo(smallArray, smallArray.Length) or CopyTo(arr, arr.Length+1) — i.e. a start index at/past the end of the destination array. Common with empty arrays (arrayIndex 0 is allowed for the empty-array edge case, but any positive index into a zero-length array throws).

Common situations: Passing array.Length as the start index; an arrayIndex derived from a position counter that has advanced past the end; reusing a buffer across multiple copies without resetting the index.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/baffaa4b71a67e13. Report an issue: GitHub.