{"record":{"id":"8ce42aba83f5f9fd","repo":"JamesNK/Newtonsoft.Json","slug":"the-specified-item-does-not-exist-in-this-keyedcol","errorCode":null,"errorMessage":"The specified item does not exist in this KeyedCollection.","messagePattern":"The specified item does not exist in this KeyedCollection\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs","lineNumber":55,"sourceCode":"        private static readonly IEqualityComparer<string> Comparer = StringComparer.Ordinal;\n\n        private Dictionary<string, JToken>? _dictionary;\n\n        public JPropertyKeyedCollection() : base(new List<JToken>())\n        {\n        }\n\n        private void AddKey(string key, JToken item)\n        {\n            EnsureDictionary();\n            _dictionary![key] = item;\n        }\n\n        protected void ChangeItemKey(JToken item, string newKey)\n        {\n            if (!ContainsItem(item))\n            {\n                throw new ArgumentException(\"The specified item does not exist in this KeyedCollection.\");\n            }\n\n            string keyForItem = GetKeyForItem(item);\n            if (!Comparer.Equals(keyForItem, newKey))\n            {\n                if (newKey != null)\n                {\n                    AddKey(newKey, item);\n                }\n\n                if (keyForItem != null)\n                {\n                    RemoveKey(keyForItem);\n                }\n            }\n        }\n\n        protected override void ClearItems()","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JPropertyKeyedCollection.cs#L37-L73","documentation":"JPropertyKeyedCollection (the lookup store behind JObject) throws ArgumentException 'The specified item does not exist in this KeyedCollection.' from ChangeItemKey (JPropertyKeyedCollection.cs:55) when a key change is requested for a token that is not present in the backing dictionary. It protects dictionary/list synchronization during property renames.","triggerScenarios":"Renaming a JObject property (e.g. JObject's internal rename path) when the target JProperty was never inserted, was already removed, or the collection's dictionary was not yet initialized (too few items to build the dictionary).","commonSituations":"Renaming properties on an empty or freshly-manipulated JObject before its internal dictionary exists; concurrent/desynced mutation where a property is removed mid-rename; direct reflection into the internal collection.","solutions":["Ensure the property actually exists in the JObject before renaming (check via obj.Property(name) != null).","Avoid mutating the JObject from multiple threads; JObject is not thread-safe for concurrent writes.","Do not reach into the internal keyed collection via reflection — use the public JObject API."],"exampleFix":"// before: rename with no guarantee the property exists\n((JProperty)obj.First).Set(value); // can desync internals under manual key changes\n\n// after: rename via the public, validated path\nvar prop = obj.Property(\"oldName\");\nif (prop != null)\n{\n    var value = prop.Value;\n    obj.Remove(\"oldName\");\n    obj.Add(\"newName\", value);\n}","handlingStrategy":"validation","validationCode":"// Verify the property exists before any rename, and use the public API.\nvar prop = obj.Property(\"oldName\");\nif (prop != null)\n{\n    var value = prop.Value;\n    obj.Remove(\"oldName\");\n    obj.Add(\"newName\", value);\n}","typeGuard":"static bool PropertyExists(JObject o, string name) => o?.Property(name) != null;","tryCatchPattern":null,"preventionTips":["Do not reach into JObject's internal keyed collection via reflection.","Do not mutate a JObject concurrently from multiple threads.","Rename via remove+add through the public surface, after an existence check."],"tags":["json","linq-to-json","jobject","collection","internal"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}