JamesNK/Newtonsoft.Json · error · ArgumentException

Accessed JObject values with invalid key value: {0}. Object

Error message

Accessed JObject values with invalid key value: {0}. Object property name expected.

What it means

ArgumentException thrown by the JObject this[object key] getter when the supplied key is non-null but not a string. JObject property access by object key requires a string property name; any other key type (int, Guid, JValue, etc.) is rejected.

Source

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

        /// <returns>A <see cref="JEnumerable{T}"/> of <see cref="JToken"/> of this object's property values.</returns>
        public JEnumerable<JToken> PropertyValues()
        {
            return new JEnumerable<JToken>(Properties().Select(p => p.Value));
        }

        /// <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 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>

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Ensure the key is a string: call jObject[key.ToString()] or cast (string)key when you have a name.
  2. Use the strongly-typed this[string propertyName] indexer, which makes the requirement explicit at compile time.
  3. If you intended positional access, you have a JArray — use an int indexer on the array instead.

Example fix

// before
var v = jObject[userKey]; // userKey is an int or object

// after
var v = jObject[(string)userKey];
// or
var v = jObject[Convert.ToString(userKey, CultureInfo.InvariantCulture)];
Defensive patterns

Strategy: type-guard

Validate before calling

static JToken? GetByKey(JObject obj, object key)
{
    if (key is not string name)
        throw new ArgumentException("Key must be a string property name.", nameof(key));
    return obj[name];
}

Type guard

static bool IsPropertyNameKey(object? key) => key is string;

Try / catch

try { return jObject[key]; }
catch (ArgumentException ex) when (ex.Message.Contains("invalid key value"))
{
    return jObject[Convert.ToString(key, CultureInfo.InvariantCulture)!];
}

Prevention

When it happens

Trigger: Calling jObject[someInt], jObject[guid], jObject[anotherJToken], or any non-string key on the object indexer getter — commonly when a key variable is typed as object and happens to hold a non-string.

Common situations: Generic dictionary-style code that keys into JObject with object; confusing JObject indexing (string keys) with JArray indexing (int keys); passing enum/object keys.

Related errors


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