{"record":{"id":"5be1b8f458b56f07","repo":"JamesNK/Newtonsoft.Json","slug":"accessed-jobject-values-with-invalid-key-value-0","errorCode":null,"errorMessage":"Accessed JObject values with invalid key value: {0}. Object property name expected.","messagePattern":"Accessed JObject values with invalid key value: (.+?)\\. Object property name expected\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JObject.cs","lineNumber":339,"sourceCode":"        /// <returns>A <see cref=\"JEnumerable{T}\"/> of <see cref=\"JToken\"/> of this object's property values.</returns>\n        public JEnumerable<JToken> PropertyValues()\n        {\n            return new JEnumerable<JToken>(Properties().Select(p => p.Value));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"JToken\"/> with the specified key.\n        /// </summary>\n        /// <value>The <see cref=\"JToken\"/> with the specified key.</value>\n        public override JToken? this[object key]\n        {\n            get\n            {\n                ValidationUtils.ArgumentNotNull(key, nameof(key));\n\n                if (!(key is string propertyName))\n                {\n                    throw new ArgumentException(\"Accessed JObject values with invalid key value: {0}. Object property name expected.\".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));\n                }\n\n                return this[propertyName];\n            }\n            set\n            {\n                ValidationUtils.ArgumentNotNull(key, nameof(key));\n\n                if (!(key is string propertyName))\n                {\n                    throw new ArgumentException(\"Set JObject values with invalid key value: {0}. Object property name expected.\".FormatWith(CultureInfo.InvariantCulture, MiscellaneousUtils.ToString(key)));\n                }\n\n                this[propertyName] = value;\n            }\n        }\n\n        /// <summary>","sourceCodeStart":321,"sourceCodeEnd":357,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JObject.cs#L321-L357","documentation":"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.","triggerScenarios":"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.","commonSituations":"Generic dictionary-style code that keys into JObject with object; confusing JObject indexing (string keys) with JArray indexing (int keys); passing enum/object keys.","solutions":["Ensure the key is a string: call jObject[key.ToString()] or cast (string)key when you have a name.","Use the strongly-typed this[string propertyName] indexer, which makes the requirement explicit at compile time.","If you intended positional access, you have a JArray — use an int indexer on the array instead."],"exampleFix":"// before\nvar v = jObject[userKey]; // userKey is an int or object\n\n// after\nvar v = jObject[(string)userKey];\n// or\nvar v = jObject[Convert.ToString(userKey, CultureInfo.InvariantCulture)];","handlingStrategy":"type-guard","validationCode":"static JToken? GetByKey(JObject obj, object key)\n{\n    if (key is not string name)\n        throw new ArgumentException(\"Key must be a string property name.\", nameof(key));\n    return obj[name];\n}","typeGuard":"static bool IsPropertyNameKey(object? key) => key is string;","tryCatchPattern":"try { return jObject[key]; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"invalid key value\"))\n{\n    return jObject[Convert.ToString(key, CultureInfo.InvariantCulture)!];\n}","preventionTips":["Type the key as string when calling the JObject indexer.","Convert object keys to string explicitly before lookup.","Do not confuse JObject (string-keyed) with JArray (int-keyed) indexing."],"tags":["newtonsoft-json","linq-to-json","jobject","indexer","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}