JamesNK/Newtonsoft.Json · error · ArgumentException
Accessed JConstructor values with invalid key value: {0}. Ar
Error message
Accessed JConstructor values with invalid key value: {0}. Argument position index expected. What it means
Thrown by the JConstructor[object key] getter when the key is not an int. JConstructor arguments are positional (unlike JProperty), so only integer argument positions are accepted.
Source
Thrown at Src/Newtonsoft.Json/Linq/JConstructor.cs:194
_values[i].WriteTo(writer, converters);
}
writer.WriteEndConstructor();
}
/// <summary>
/// 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));
if (!(key is int i))
{
throw new ArgumentException("Accessed JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
}
return GetItem(i);
}
set
{
ValidationUtils.ArgumentNotNull(key, nameof(key));
if (!(key is int i))
{
throw new ArgumentException("Set JConstructor values with invalid key value: {0}. Argument position index expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
}
SetItem(i, value);
}
}
internal override int GetDeepHashCode()View on GitHub (pinned to 4f73e74372)
Solutions
- Use an integer argument position: ctor[0].
- Iterate Children() to find a specific argument by value.
- Re-check whether you actually need a JObject for named access.
Example fix
// before JToken a = myCtor[(object)"x"]; // after JToken a = myCtor[0];
Defensive patterns
Strategy: type-guard
Validate before calling
if (key is int i) { JToken v = ctor[i]; }
else throw new InvalidOperationException("JConstructor requires an int argument index."); Type guard
static bool IsCtorKey(object key) => key is int;
Try / catch
try { var v = ctor[key]; }
catch (ArgumentException ex) when (ex.Message.Contains("Argument position index expected")) {
// fall back to integer index
} Prevention
- Always use integer positions for JConstructor arguments.
- Remember JConstructor is positional, not keyed.
- Wrap dynamic keys in an int-coercion check.
When it happens
Trigger: Indexing a JConstructor with a string or any non-int key on the read side.
Common situations: Assuming JConstructor supports named lookup like JObject; dynamic key dispatch.
Related errors
- Set JConstructor values with invalid key value: {0}. Argumen
- Accessed JArray values with invalid key value: {0}. Int32 ar
- Set JArray values with invalid key value: {0}. Int32 array i
- Constructor name cannot be empty.
- An ObjectId must be 12 bytes
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/3da9929ab8a380d2.
Report an issue: GitHub.