JamesNK/Newtonsoft.Json · error · ArgumentException
Accessed JArray values with invalid key value: {0}. Int32 ar
Error message
Accessed JArray values with invalid key value: {0}. Int32 array index expected. What it means
Thrown by the JArray[object key] getter when the key is neither an int nor (on .NET 6+) a System.Index. JArray is positional, so only integer indices resolve an element; any other key type is invalid.
Source
Thrown at Src/Newtonsoft.Json/Linq/JArray.cs:260
/// Gets the <see cref="JToken"/> with the specified key.
/// </summary>
/// <value>The <see cref="JToken"/> with the specified key.</value>
public override JToken? this[object key]
{
get
{
ValidationUtils.ArgumentNotNull(key, nameof(key));
switch (key)
{
case int intKey:
return GetItem(intKey);
#if NET6_0_OR_GREATER
case Index indexKey:
return GetItem(indexKey.GetOffset(Count));
#endif
default:
throw new ArgumentException("Accessed JArray values with invalid key value: {0}. Int32 array index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
}
}
set
{
ValidationUtils.ArgumentNotNull(key, nameof(key));
switch (key)
{
case int intKey:
SetItem(intKey, value);
return;
#if NET6_0_OR_GREATER
case Index indexKey:
SetItem(indexKey.GetOffset(Count), value);
return;
#endif
default:View on GitHub (pinned to 4f73e74372)
Solutions
- Use the strongly-typed int indexer: arr[intIndex].
- If you need lookup by content, use LINQ (arr.First(t => ...)) or the First/Last properties.
- If a string key was intended, parse the JSON as JObject instead of JArray.
Example fix
// before JToken v = myArray[(object)"id"]; // after JToken v = myArray[0];
Defensive patterns
Strategy: type-guard
Validate before calling
if (key is int i) { JToken v = arr[i]; }
else throw new InvalidOperationException("JArray requires an int index."); Type guard
static bool IsArrayKey(object key) => key is int
#if NET6_0_OR_GREATER
|| key is System.Index
#endif
; Try / catch
try { var v = arr[key]; }
catch (ArgumentException ex) when (ex.Message.Contains("Int32 array index expected")) {
// resolve via LINQ or convert key to int
} Prevention
- Prefer the int indexer arr[int] over the object indexer.
- Don't reuse JObject string-key code on JArray.
- When key arrives via dynamic, coerce to int before indexing.
When it happens
Trigger: Indexing with a string (arr["name"]), a long, a double, or any non-int object via the object-key indexer.
Common situations: Code written for JObject (string keys) reused on a JArray; passing a dynamic/boxed value whose runtime type is long or byte rather than int.
Related errors
- Set JArray values with invalid key value: {0}. Int32 array i
- Object serialized to {0}. JArray instance expected.
- Accessed JConstructor values with invalid key value: {0}. Ar
- Set JConstructor values with invalid key value: {0}. Argumen
- An ObjectId must be 12 bytes
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/eee0c0445c653278.
Report an issue: GitHub.