{"record":{"id":"02b668d92fe9fbe8","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-add-0-to-1-02b668","errorCode":null,"errorMessage":"Can not add {0} to {1}.","messagePattern":"Can not add (.+?) to (.+?)\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JObject.cs","lineNumber":160,"sourceCode":"\n        internal override bool InsertItem(int index, JToken? item, bool skipParentCheck, bool copyAnnotations)\n        {\n            // don't add comments to JObject, no name to reference comment by\n            if (item != null && item.Type == JTokenType.Comment)\n            {\n                return false;\n            }\n\n            return base.InsertItem(index, item, skipParentCheck, copyAnnotations);\n        }\n\n        internal override void ValidateToken(JToken o, JToken? existing)\n        {\n            ValidationUtils.ArgumentNotNull(o, nameof(o));\n\n            if (o.Type != JTokenType.Property)\n            {\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            }","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JObject.cs#L142-L178","documentation":"ArgumentException thrown by JObject.ValidateToken when attempting to add a JToken that is not a JProperty. JObject may only contain JProperty children; adding any other token type (a JValue, JObject, JArray, or a literal wrapped via JValue) is rejected.","triggerScenarios":"Calling jObject.Add(new JValue(...)), jObject.Add(otherJObject), jObject.Add(jArray), ((IList<JToken>)jObject).Add(someValue), or JContainer.Add on a JObject with a non-JProperty item. Also triggered by jObject[\"key\"] = someNonJToken routed through paths that build a property incorrectly.","commonSituations":"Treating a JObject like a JArray and pushing values directly; copying children from one container to a JObject without wrapping in JProperty; generic 'merge into object' helpers that add raw tokens.","solutions":["Wrap the value in a named property: jObject.Add(new JProperty(\"name\", value)).","Use the string-keyed Add overload: jObject.Add(\"name\", value) which constructs the JProperty for you.","Use the indexer jObject[\"name\"] = value to set/replace a property."],"exampleFix":"// before\njObject.Add(new JValue(42)); // ArgumentException\n\n// after\njObject.Add(\"age\", 42);\n// or explicitly\njObject.Add(new JProperty(\"age\", 42));","handlingStrategy":"type-guard","validationCode":"static void AddProperty(JObject obj, string name, JToken value)\n{\n    var prop = value as JProperty ?? new JProperty(name, value);\n    obj.Add(prop);\n}","typeGuard":"static bool IsProperty(JToken? t) => t is JProperty;","tryCatchPattern":"try { jObject.Add(item); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Can not add \"))\n{\n    jObject.Add(new JProperty(\"item\", item));\n}","preventionTips":["Only add JProperty instances to JObject; use Add(name, value) for convenience.","Use the string indexer to set properties.","When copying children between containers, match container type to child type."],"tags":["newtonsoft-json","linq-to-json","jobject","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}