{"record":{"id":"c9ee5036e46a0a40","repo":"JamesNK/Newtonsoft.Json","slug":"set-jobject-values-with-invalid-key-value-0-ob","errorCode":null,"errorMessage":"Set JObject values with invalid key value: {0}. Object property name expected.","messagePattern":"Set 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":350,"sourceCode":"        {\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>\n        /// Gets or sets the <see cref=\"JToken\"/> with the specified property name.\n        /// </summary>\n        /// <value></value>\n        public JToken? this[string propertyName]\n        {\n            get\n            {\n                ValidationUtils.ArgumentNotNull(propertyName, nameof(propertyName));\n\n                JProperty? property = Property(propertyName, StringComparison.Ordinal);\n","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JObject.cs#L332-L368","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\njObject[userKey] = value; // userKey is int/object\n\n// after\njObject[(string)userKey] = value;","handlingStrategy":"type-guard","validationCode":"static void SetByKey(JObject obj, object key, JToken? value)\n{\n    if (key is not string name)\n        throw new ArgumentException(\"Key must be a string property name.\", nameof(key));\n    obj[name] = value;\n}","typeGuard":"static bool IsPropertyNameKey(object? key) => key is string;","tryCatchPattern":"try { jObject[key] = value; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"invalid key value\"))\n{\n    jObject[Convert.ToString(key, CultureInfo.InvariantCulture)!] = value;\n}","preventionTips":["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."],"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"}