JamesNK/Newtonsoft.Json · error · ArgumentException
Set JObject values with invalid key value: {0}. Object prope
Error message
Set JObject values with invalid key value: {0}. Object property name expected. What it means
ArgumentException thrown by the JObject this[object key] setter when the supplied key is non-null but not a string. Setting a property via the object indexer requires a string property name; any other key type is rejected.
Source
Thrown at Src/Newtonsoft.Json/Linq/JObject.cs:350
{
get
{
ValidationUtils.ArgumentNotNull(key, nameof(key));
if (!(key is string propertyName))
{
throw new ArgumentException("Accessed JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
}
return this[propertyName];
}
set
{
ValidationUtils.ArgumentNotNull(key, nameof(key));
if (!(key is string propertyName))
{
throw new ArgumentException("Set JObject values with invalid key value: {0}. Object property name expected.".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));
}
this[propertyName] = value;
}
}
/// <summary>
/// Gets or sets the <see cref="JToken"/> with the specified property name.
/// </summary>
/// <value></value>
public JToken? this[string propertyName]
{
get
{
ValidationUtils.ArgumentNotNull(propertyName, nameof(propertyName));
JProperty? property = Property(propertyName, StringComparison.Ordinal);
View on GitHub (pinned to 4f73e74372)
Solutions
- Ensure the key is a string before assignment: jObject[(string)key] = value.
- Use the strongly-typed this[string propertyName] setter.
- Validate the key type upstream if it comes from untyped data.
Example fix
// before jObject[userKey] = value; // userKey is int/object // after jObject[(string)userKey] = value;
Defensive patterns
Strategy: type-guard
Validate before calling
static void SetByKey(JObject obj, object key, JToken? value)
{
if (key is not string name)
throw new ArgumentException("Key must be a string property name.", nameof(key));
obj[name] = value;
} Type guard
static bool IsPropertyNameKey(object? key) => key is string;
Try / catch
try { jObject[key] = value; }
catch (ArgumentException ex) when (ex.Message.Contains("invalid key value"))
{
jObject[Convert.ToString(key, CultureInfo.InvariantCulture)!] = value;
} Prevention
- Type the key as string when setting via the indexer.
- Convert object keys to string explicitly before assignment.
- Validate key types in generic dictionary-write helpers.
When it happens
Trigger: Assigning jObject[someInt] = value, jObject[guid] = value, or any non-string key on the object indexer setter — typically when a loosely-typed key variable holds a non-string.
Common situations: Generic code that writes into JObject via object keys; reflection/dynamic dispatch where the key type is not enforced; mixing array-style and object-style indexing.
Related errors
- Accessed JObject values with invalid key value: {0}. Object
- Can not add {0} to {1}.
- Object serialized to {0}. JObject instance expected.
- Argument is not a JToken.
- New item to be added to collection must be compatible with {
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/c9ee5036e46a0a40.
Report an issue: GitHub.