{"record":{"id":"fd47c9257db7c254","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-add-property-0-to-1-property-with-the","errorCode":null,"errorMessage":"Can not add property {0} to {1}. Property with the same name already exists on object.","messagePattern":"Can not add property (.+?) to (.+?)\\. Property with the same name already exists on object\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JObject.cs","lineNumber":177,"sourceCode":"            {\n                throw new ArgumentException(\"Can not add {0} to {1}.\".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));\n            }\n\n            JProperty newProperty = (JProperty)o;\n\n            if (existing != null)\n            {\n                JProperty existingProperty = (JProperty)existing;\n\n                if (newProperty.Name == existingProperty.Name)\n                {\n                    return;\n                }\n            }\n\n            if (_properties.TryGetValue(newProperty.Name, out existing))\n            {\n                throw new ArgumentException(\"Can not add property {0} to {1}. Property with the same name already exists on object.\".FormatWith(CultureInfo.InvariantCulture, newProperty.Name, GetType()));\n            }\n        }\n\n        internal override void MergeItem(object content, JsonMergeSettings? settings)\n        {\n            if (!(content is JObject o))\n            {\n                return;\n            }\n\n            foreach (KeyValuePair<string, JToken?> contentItem in o)\n            {\n                JProperty? existingProperty = Property(contentItem.Key, settings?.PropertyNameComparison ?? StringComparison.Ordinal);\n\n                if (existingProperty == null)\n                {\n                    Add(contentItem.Key, contentItem.Value);\n                }","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JObject.cs#L159-L195","documentation":"ArgumentException thrown by JObject.ValidateToken when adding a JProperty whose name already exists in the JObject. By default JObject does not allow duplicate property names; during normal Add/Insert (not merge) a name collision is rejected.","triggerScenarios":"Calling jObject.Add(new JProperty(\"x\", 1)) when 'x' is already present; building a JObject programmatically and inserting two properties with the same name; copying properties from another object without checking for existing keys.","commonSituations":"Aggregating properties from multiple sources into one JObject; loops that add keys derived from data containing duplicates; converting dictionaries that may share keys.","solutions":["Check existence first: if (jObject[\"x\"] == null) jObject.Add(\"x\", value); or use Property(name) == null.","Use the indexer jObject[\"x\"] = value to replace an existing property instead of Add.","Use Merge with JsonMergeSettings if you need to combine two JObjects (Merge replaces/merges duplicates rather than throwing)."],"exampleFix":"// before\njObject.Add(new JProperty(\"id\", 1));\njObject.Add(new JProperty(\"id\", 2)); // ArgumentException\n\n// after\njObject[\"id\"] = 2; // replaces\n// or\nif (jObject.Property(\"id\") == null) jObject.Add(\"id\", 2);","handlingStrategy":"validation","validationCode":"static void Put(JObject obj, string name, JToken value)\n{\n    if (obj.Property(name) != null) obj[name] = value;\n    else obj.Add(name, value);\n}","typeGuard":"static bool HasProperty(JObject obj, string name) => obj.Property(name) != null;","tryCatchPattern":"try { jObject.Add(new JProperty(name, value)); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"already exists\"))\n{\n    jObject[name] = value;\n}","preventionTips":["Use the indexer to set/replace properties instead of Add when duplicates are possible.","Check Property(name) == null before Add for aggregated/merged data.","Use Merge for combining JObjects rather than per-property Add."],"tags":["newtonsoft-json","linq-to-json","jobject","duplicate-key"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}