{"record":{"id":"92a285f5b876fa66","repo":"JamesNK/Newtonsoft.Json","slug":"0-cannot-have-multiple-values","errorCode":null,"errorMessage":"{0} cannot have multiple values.","messagePattern":"(.+?) cannot have multiple values\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JProperty.cs","lineNumber":261,"sourceCode":"            if (item == null)\n            {\n                return -1;\n            }\n\n            return _content.IndexOf(item);\n        }\n\n        internal override bool InsertItem(int index, JToken? item, bool skipParentCheck, bool copyAnnotations)\n        {\n            // don't add comments to JProperty\n            if (item != null && item.Type == JTokenType.Comment)\n            {\n                return false;\n            }\n\n            if (Value != null)\n            {\n                throw new JsonException(\"{0} cannot have multiple values.\".FormatWith(CultureInfo.InvariantCulture, typeof(JProperty)));\n            }\n\n            return base.InsertItem(0, item, false, copyAnnotations);\n        }\n\n        internal override bool ContainsItem(JToken? item)\n        {\n            return (Value == item);\n        }\n\n        internal override void MergeItem(object content, JsonMergeSettings? settings)\n        {\n            JToken? value = (content as JProperty)?.Value;\n\n            if (value != null && value.Type != JTokenType.Null)\n            {\n                Value = value;\n            }","sourceCodeStart":243,"sourceCodeEnd":279,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JProperty.cs#L243-L279","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar prop = new JProperty(\"items\", 1);\nprop.Add(new JValue(2)); // throws JsonException: JProperty cannot have multiple values\n\n// after — make the value an array\nvar prop = new JProperty(\"items\", new JArray(1, 2));","handlingStrategy":"validation","validationCode":"// A property holds one value; assign rather than Add.\nif (token is JProperty p) p.Value = newValue;\nelse if (token is JArray a) a.Add(newValue); // multi-value goes in an array","typeGuard":"static bool CanHoldMultipleValues(JToken t) => t is JArray or JObject;","tryCatchPattern":null,"preventionTips":["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."],"tags":["json","linq-to-json","jproperty","collection","mutation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}