JamesNK/Newtonsoft.Json · error · ArgumentException
Can not add property {0} to {1}. Property with the same name
Error message
Can not add property {0} to {1}. Property with the same name already exists on object. What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Linq/JObject.cs:177
{
throw new ArgumentException("Can not add {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
}
JProperty newProperty = (JProperty)o;
if (existing != null)
{
JProperty existingProperty = (JProperty)existing;
if (newProperty.Name == existingProperty.Name)
{
return;
}
}
if (_properties.TryGetValue(newProperty.Name, out existing))
{
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()));
}
}
internal override void MergeItem(object content, JsonMergeSettings? settings)
{
if (!(content is JObject o))
{
return;
}
foreach (KeyValuePair<string, JToken?> contentItem in o)
{
JProperty? existingProperty = Property(contentItem.Key, settings?.PropertyNameComparison ?? StringComparison.Ordinal);
if (existingProperty == null)
{
Add(contentItem.Key, contentItem.Value);
}View on GitHub (pinned to 4f73e74372)
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).
Example fix
// before
jObject.Add(new JProperty("id", 1));
jObject.Add(new JProperty("id", 2)); // ArgumentException
// after
jObject["id"] = 2; // replaces
// or
if (jObject.Property("id") == null) jObject.Add("id", 2); Defensive patterns
Strategy: validation
Validate before calling
static void Put(JObject obj, string name, JToken value)
{
if (obj.Property(name) != null) obj[name] = value;
else obj.Add(name, value);
} Type guard
static bool HasProperty(JObject obj, string name) => obj.Property(name) != null;
Try / catch
try { jObject.Add(new JProperty(name, value)); }
catch (ArgumentException ex) when (ex.Message.Contains("already exists"))
{
jObject[name] = value;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Aggregating properties from multiple sources into one JObject; loops that add keys derived from data containing duplicates; converting dictionaries that may share keys.
Related errors
- Can not add {0} to {1}.
- Accessed JObject values with invalid key value: {0}. Object
- Set JObject values with invalid key value: {0}. Object prope
- Object serialized to {0}. JObject instance expected.
- array
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/fd47c9257db7c254.
Report an issue: GitHub.