JamesNK/Newtonsoft.Json · error · JsonException
{0} cannot have multiple values.
Error message
{0} cannot have multiple values. What it means
A JSON property is a single key with exactly one value, so JProperty.InsertItem (JProperty.cs:261) throws JsonException 'JProperty cannot have multiple values.' when an insert is attempted while a value already exists. This prevents constructing a property like { "a": 1 2 }, which has no valid JSON representation.
Source
Thrown at Src/Newtonsoft.Json/Linq/JProperty.cs:261
if (item == null)
{
return -1;
}
return _content.IndexOf(item);
}
internal override bool InsertItem(int index, JToken? item, bool skipParentCheck, bool copyAnnotations)
{
// don't add comments to JProperty
if (item != null && item.Type == JTokenType.Comment)
{
return false;
}
if (Value != null)
{
throw new JsonException("{0} cannot have multiple values.".FormatWith(CultureInfo.InvariantCulture, typeof(JProperty)));
}
return base.InsertItem(0, item, false, copyAnnotations);
}
internal override bool ContainsItem(JToken? item)
{
return (Value == item);
}
internal override void MergeItem(object content, JsonMergeSettings? settings)
{
JToken? value = (content as JProperty)?.Value;
if (value != null && value.Type != JTokenType.Null)
{
Value = value;
}View on GitHub (pinned to 4f73e74372)
Solutions
- Replace the existing value: myProperty.Value = newValue (do not Add).
- If you need a list of values, model it as a property whose value is a JArray, or use a JObject with multiple properties.
- Check myProperty.Value == null before adding if you genuinely intend first-time assignment.
Example fix
// before
var prop = new JProperty("items", 1);
prop.Add(new JValue(2)); // throws JsonException: JProperty cannot have multiple values
// after — make the value an array
var prop = new JProperty("items", new JArray(1, 2)); Defensive patterns
Strategy: validation
Validate before calling
// A property holds one value; assign rather than Add. if (token is JProperty p) p.Value = newValue; else if (token is JArray a) a.Add(newValue); // multi-value goes in an array
Type guard
static bool CanHoldMultipleValues(JToken t) => t is JArray or JObject;
Prevention
- Use Value = to replace; never Add a second child to a JProperty.
- Model lists as a property whose value is a JArray.
- Check p.Value == null before any first-time Add if assignment is intentional.
When it happens
Trigger: Calling Add()/AddFirst()/AddBeforeSelf() on a JProperty that already has a value (e.g. myProperty.Add(extraToken)), or routing multi-content into a property's child list.
Common situations: Appending extra values to a property during tree construction; merging content that unintentionally targets an already-valued property; confusing a JProperty with a JArray when building nodes programmatically.
Related errors
- Cannot add or remove items from {0}.
- The specified item does not exist in this KeyedCollection.
- The parent is missing.
- key
- Cannot access child value on {0}.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/92a285f5b876fa66.
Report an issue: GitHub.