JamesNK/Newtonsoft.Json · error · InvalidOperationException

Cannot access child value on {0}.

Error message

Cannot access child value on {0}.

What it means

The base JToken.this[object key] getter (JToken.cs:355) throws InvalidOperationException 'Cannot access child value on {type}.' because only containers (JObject/JArray/JProperty) support keyed child access. A leaf token such as JValue (string/number/bool) has no children, so indexing it is meaningless.

Source

Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:355

        {
            if (Parent == null)
            {
                yield break;
            }

            for (JToken? o = Parent.First; o != this && o != null; o = o.Next)
            {
                yield return o;
            }
        }

        /// <summary>
        /// Gets the <see cref="JToken"/> with the specified key.
        /// </summary>
        /// <value>The <see cref="JToken"/> with the specified key.</value>
        public virtual JToken? this[object key]
        {
            get => throw new InvalidOperationException("Cannot access child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
            set => throw new InvalidOperationException("Cannot set child value on {0}.".FormatWith(CultureInfo.InvariantCulture, GetType()));
        }

        /// <summary>
        /// Gets the <see cref="JToken"/> with the specified key converted to the specified type.
        /// </summary>
        /// <typeparam name="T">The type to convert the token to.</typeparam>
        /// <param name="key">The token key.</param>
        /// <returns>The converted token value.</returns>
        public virtual T? Value<T>(object key)
        {
            JToken? token = this[key];

            // null check to fix MonoTouch issue - https://github.com/dolbz/Newtonsoft.Json/commit/a24e3062846b30ee505f3271ac08862bb471b822
            return token == null ? default : Extensions.Convert<JToken, T>(token);
        }

        /// <summary>

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Check token.Type (or token is JObject / is JArray) before indexing.
  2. Use token.Value<T>(key) which safely returns default for absent/leaf cases.
  3. Validate the JSON shape against a schema before generic navigation.

Example fix

// before
JToken node = obj["data"];        // actually a scalar in the data
var inner = node["id"];           // throws InvalidOperationException: Cannot access child value on JValue

// after
if (node is JObject objNode)
{
    var inner = objNode["id"];
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (node is JObject objNode) { var inner = objNode[key]; }

Type guard

static bool IsIndexableContainer(JToken t) => t is JObject or JArray;

Prevention

When it happens

Trigger: Indexing a non-container token, e.g. someJValue["key"] or token[i] where the runtime type is JValue/JRaw rather than JObject/JArray; navigating a tree where a node you assumed was an object turned out to be a scalar.

Common situations: Assuming a field is always an object when the JSON sometimes has a scalar/null there; weakly-typed traversal where the expected container shape does not match the data.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/3ca9854c64acdbdb. Report an issue: GitHub.