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

CopyItemsTo throws ArgumentException when arrayIndex is >= array.Length and is not exactly 0. The special allowance for 0 lets CopyTo succeed into a zero-length array only when no offset is requested; any other out-of-range offset is rejected.

Source

Thrown at Src/Newtonsoft.Json/Linq/JContainer.cs:601

        internal virtual bool ContainsItem(JToken? item)
        {
            return (IndexOfItem(item) != -1);
        }

        internal virtual void CopyItemsTo(Array 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 (JToken token in ChildrenTokens)
            {
                array.SetValue(token, arrayIndex + index);
                index++;
            }
        }

        internal static bool IsTokenUnchanged(JToken currentValue, JToken? newValue)
        {
            if (currentValue is JValue v1)
            {

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Size the destination as container.Count + arrayIndex.
  2. Ensure arrayIndex < array.Length (or use 0).
  3. Recompute the destination length immediately before copying.

Example fix

// before
arr.CopyTo(dest, dest.Length);
// after
var dest = new JToken[arr.Count];
arr.CopyTo(dest, 0);
Defensive patterns

Strategy: validation

Validate before calling

if (arrayIndex != 0 && arrayIndex >= dest.Length) throw new ArgumentException("arrayIndex out of range.");
container.CopyTo(dest, arrayIndex);

Try / catch

try { container.CopyTo(dest, arrayIndex); }
catch (ArgumentException ex) when (ex.Message.Contains("length of array")) {
    // resize dest or reset arrayIndex to 0
    Array.Resize(ref dest, arrayIndex + container.Count);
    container.CopyTo(dest, arrayIndex);
}

Prevention

When it happens

Trigger: jArray.CopyTo(dest, dest.Length); passing an offset equal to or beyond the array length (other than 0).

Common situations: Destination array sized exactly without room for the offset; stale length calculation.

Related errors


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