{"record":{"id":"006d246cd61c2930","repo":"JamesNK/Newtonsoft.Json","slug":"cannot-add-or-remove-items-from-0","errorCode":null,"errorMessage":"Cannot add or remove items from {0}.","messagePattern":"Cannot add or remove items from (.+?)\\.","errorType":"exception","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JProperty.cs","lineNumber":233,"sourceCode":"            {\n                throw new ArgumentOutOfRangeException();\n            }\n\n            if (IsTokenUnchanged(Value, item))\n            {\n                return;\n            }\n\n            ((JObject?)Parent)?.InternalPropertyChanging(this);\n\n            base.SetItem(0, item);\n\n            ((JObject?)Parent)?.InternalPropertyChanged(this);\n        }\n\n        internal override bool RemoveItem(JToken? item)\n        {\n            throw new JsonException(\"Cannot add or remove items from {0}.\".FormatWith(CultureInfo.InvariantCulture, typeof(JProperty)));\n        }\n\n        internal override void RemoveItemAt(int index)\n        {\n            throw new JsonException(\"Cannot add or remove items from {0}.\".FormatWith(CultureInfo.InvariantCulture, typeof(JProperty)));\n        }\n\n        internal override int IndexOfItem(JToken? item)\n        {\n            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)","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JProperty.cs#L215-L251","documentation":"A JProperty must always have exactly one value, so removing that value (rather than replacing it) is illegal. JProperty.RemoveItem (JProperty.cs:233) throws JsonException 'Cannot add or remove items from JProperty.' when something attempts to delete the property's sole child. To change the value you must assign a new one; to remove the whole property you remove the property from its parent JObject.","triggerScenarios":"Calling .Remove() on the value token held by a property (e.g. myProperty.Value.Remove() or myProperty.First.Remove()), or invoking Remove(item) on the property's child collection through JContainer APIs.","commonSituations":"Walking a parsed JSON tree and calling Remove() on leaf nodes generically; conditional deletion logic that tries to null out a property by deleting its value instead of setting Value = null or removing the property from the owning object.","solutions":["To blank the value, set myProperty.Value = null (or JValue.CreateNull()) instead of removing the child.","To delete the property entirely, call myProperty.Remove() (removes the property from its parent JObject) or parentObject.Remove(\"name\").","In generic tree pruning, skip the value inside a JProperty and act on the property itself."],"exampleFix":"// before\nvar prop = (JProperty)obj.First;\nprop.Value.Remove(); // throws JsonException: Cannot add or remove items from JProperty\n\n// after — blank the value\nprop.Value = null;\n// after — delete the property from its parent\nprop.Remove();","handlingStrategy":"validation","validationCode":"// To blank a value: set Value = null. To delete the property: remove it from the parent.\nif (token is JProperty p)\n{\n    p.Value = null;       // blank, keep property\n    // or p.Remove();      // delete property from its JObject\n}","typeGuard":"static bool HasParent(JToken t) => t?.Parent != null;","tryCatchPattern":"try { prop.Value.Remove(); }\ncatch (JsonException) { /* set Value instead */ prop.Value = null; }","preventionTips":["Never call Remove() on the value inside a JProperty; set Value = null or remove the property itself.","In generic pruning, act on the property node, not its child value.","Use parentObject.Remove(\"name\") for deletion."],"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"}