{"record":{"id":"7582b52877f6d826","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-add-0-to-1","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/JContainer.cs","lineNumber":638,"sourceCode":"                if (newValue == null)\n                {\n                    // null will get turned into a JValue of type null\n                    return v1.Type == JTokenType.Null;\n                }\n\n                return v1.Equals(newValue);\n            }\n\n            return false;\n        }\n\n        internal virtual 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\n        /// <summary>\n        /// Adds the specified content as children of this <see cref=\"JToken\"/>.\n        /// </summary>\n        /// <param name=\"content\">The content to be added.</param>\n        public virtual void Add(object? content)\n        {\n            TryAddInternal(ChildrenTokens.Count, content, false, copyAnnotations: true);\n        }\n\n        internal bool TryAdd(object? content)\n        {\n            return TryAddInternal(ChildrenTokens.Count, content, false, copyAnnotations: true);\n        }\n\n        internal void AddAndSkipParentCheck(JToken token)","sourceCodeStart":620,"sourceCodeEnd":656,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JContainer.cs#L620-L656","documentation":"ValidateToken throws ArgumentException when you attempt to add a JProperty token to a non-object container (a JArray or JConstructor). JProperty is only valid as a child of JObject; arrays and constructors hold values, not name/value pairs.","triggerScenarios":"jArray.Add(new JProperty(\"k\", 1)); inserting a JProperty via InsertItem into a JArray/JConstructor.","commonSituations":"Generic tree-building code that adds JProperty nodes indiscriminately; converting object subtrees into array children without unwrapping.","solutions":["Add the property's Value (not the JProperty) to a JArray: jArray.Add(prop.Value).","Add JProperty nodes only to a JObject.","If you need name/value pairs, build a JObject, not a JArray."],"exampleFix":"// before\njArray.Add(new JProperty(\"id\", 1));\n// after\njArray.Add(1);\n// or, for named pairs, use a JObject:\n// jObject.Add(new JProperty(\"id\", 1));","handlingStrategy":"validation","validationCode":"if (token is JProperty prop) {\n    // unwrap for non-object containers, or add to a JObject\n    jArray.Add(prop.Value);\n} else {\n    jArray.Add(token);\n}","typeGuard":"static bool IsValidArrayChild(JToken t) => t.Type != JTokenType.Property;","tryCatchPattern":"try { jArray.Add(token); }\ncatch (ArgumentException ex) when (ex.Message.StartsWith(\"Can not add\")) {\n    // token was a JProperty: add its value instead\n    if (token is JProperty p) jArray.Add(p.Value);\n}","preventionTips":["Only add JProperty nodes to JObject instances.","Unwrap JProperty.Value when inserting into a JArray/JConstructor.","Centralize token creation so Property tokens never reach array builders."],"tags":["jcontainer","jproperty","validate","argument"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}