{"record":{"id":"ca59dd750a4c1aed","repo":"JamesNK/Newtonsoft.Json","slug":"the-parent-is-missing","errorCode":null,"errorMessage":"The parent is missing.","messagePattern":"The parent is missing\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":267,"sourceCode":"#endif\n\n                return JsonPosition.BuildPath(positions, null);\n            }\n        }\n\n        internal JToken()\n        {\n        }\n\n        /// <summary>\n        /// Adds the specified content immediately after this token.\n        /// </summary>\n        /// <param name=\"content\">A content object that contains simple content or a collection of content objects to be added after this token.</param>\n        public void AddAfterSelf(object? content)\n        {\n            if (_parent == null)\n            {\n                throw new InvalidOperationException(\"The parent is missing.\");\n            }\n\n            int index = _parent.IndexOfItem(this);\n            _parent.TryAddInternal(index + 1, content, false, copyAnnotations: true);\n        }\n\n        /// <summary>\n        /// Adds the specified content immediately before this token.\n        /// </summary>\n        /// <param name=\"content\">A content object that contains simple content or a collection of content objects to be added before this token.</param>\n        public void AddBeforeSelf(object? content)\n        {\n            if (_parent == null)\n            {\n                throw new InvalidOperationException(\"The parent is missing.\");\n            }\n\n            int index = _parent.IndexOfItem(this);","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L249-L285","documentation":"JToken.AddAfterSelf (JToken.cs:267) throws InvalidOperationException 'The parent is missing.' when you try to insert a sibling next to a token that has no parent. Sibling insertion is only meaningful inside a container, so a detached token (e.g. one you just parsed or new'd up) cannot have siblings added to it.","triggerScenarios":"Calling token.AddAfterSelf(x) on a token obtained from JToken.Parse, JValue, or otherwise not attached to a JObject/JArray/JProperty; building nodes in isolation and inserting siblings before parenting them.","commonSituations":"Parsing a fragment and immediately trying to append a sibling; operating on a token that was previously removed from its tree; test scaffolding that constructs loose tokens.","solutions":["First add the token to a parent container (e.g. obj.Add(token)), then call AddAfterSelf on it.","If you want a multi-element sequence, build a JArray and Add each item to the array instead.","Check token.Parent != null before calling AddAfterSelf/AddBeforeSelf."],"exampleFix":"// before\nvar t = JToken.Parse(\"{\\\"a\\\":1}\");\nt.AddAfterSelf(new JValue(2)); // throws InvalidOperationException: parent is missing\n\n// after — attach to a container first\nvar arr = new JArray();\narr.Add(t);\nt.AddAfterSelf(new JValue(2));","handlingStrategy":"validation","validationCode":"if (token.Parent != null) token.AddAfterSelf(content);\nelse { /* attach to a container first */ new JArray(token).Add... }","typeGuard":"static bool HasParent(JToken t) => t?.Parent != null;","tryCatchPattern":null,"preventionTips":["Parent the token in a JObject/JArray before sibling insertion.","Build ordered content directly in a JArray rather than via AddAfterSelf.","Check Parent != null before AddAfterSelf/AddBeforeSelf."],"tags":["json","linq-to-json","jtoken","parent","mutation"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}