JamesNK/Newtonsoft.Json · error · InvalidOperationException
The parent is missing.
Error message
The parent is missing.
What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:267
#endif
return JsonPosition.BuildPath(positions, null);
}
}
internal JToken()
{
}
/// <summary>
/// Adds the specified content immediately after this token.
/// </summary>
/// <param name="content">A content object that contains simple content or a collection of content objects to be added after this token.</param>
public void AddAfterSelf(object? content)
{
if (_parent == null)
{
throw new InvalidOperationException("The parent is missing.");
}
int index = _parent.IndexOfItem(this);
_parent.TryAddInternal(index + 1, content, false, copyAnnotations: true);
}
/// <summary>
/// Adds the specified content immediately before this token.
/// </summary>
/// <param name="content">A content object that contains simple content or a collection of content objects to be added before this token.</param>
public void AddBeforeSelf(object? content)
{
if (_parent == null)
{
throw new InvalidOperationException("The parent is missing.");
}
int index = _parent.IndexOfItem(this);View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before
var t = JToken.Parse("{\"a\":1}");
t.AddAfterSelf(new JValue(2)); // throws InvalidOperationException: parent is missing
// after — attach to a container first
var arr = new JArray();
arr.Add(t);
t.AddAfterSelf(new JValue(2)); Defensive patterns
Strategy: validation
Validate before calling
if (token.Parent != null) token.AddAfterSelf(content);
else { /* attach to a container first */ new JArray(token).Add... } Type guard
static bool HasParent(JToken t) => t?.Parent != null;
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cannot add or remove items from {0}.
- {0} cannot have multiple values.
- Cannot access child value on {0}.
- Can not convert {0} to Single.
- Can not convert {0} to UInt32.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/ca59dd750a4c1aed.
Report an issue: GitHub.