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

  1. Replace the existing value: myProperty.Value = newValue (do not Add).
  2. If you need a list of values, model it as a property whose value is a JArray, or use a JObject with multiple properties.
  3. 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

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


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/92a285f5b876fa66. Report an issue: GitHub.