{"record":{"id":"3ca9854c64acdbdb","repo":"JamesNK/Newtonsoft.Json","slug":"cannot-access-child-value-on-0","errorCode":null,"errorMessage":"Cannot access child value on {0}.","messagePattern":"Cannot access child value on (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":355,"sourceCode":"        {\n            if (Parent == null)\n            {\n                yield break;\n            }\n\n            for (JToken? o = Parent.First; o != this && o != null; o = o.Next)\n            {\n                yield return o;\n            }\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"JToken\"/> with the specified key.\n        /// </summary>\n        /// <value>The <see cref=\"JToken\"/> with the specified key.</value>\n        public virtual JToken? this[object key]\n        {\n            get => throw new InvalidOperationException(\"Cannot access child value on {0}.\".FormatWith(CultureInfo.InvariantCulture, GetType()));\n            set => throw new InvalidOperationException(\"Cannot set child value on {0}.\".FormatWith(CultureInfo.InvariantCulture, GetType()));\n        }\n\n        /// <summary>\n        /// Gets the <see cref=\"JToken\"/> with the specified key converted to the specified type.\n        /// </summary>\n        /// <typeparam name=\"T\">The type to convert the token to.</typeparam>\n        /// <param name=\"key\">The token key.</param>\n        /// <returns>The converted token value.</returns>\n        public virtual T? Value<T>(object key)\n        {\n            JToken? token = this[key];\n\n            // null check to fix MonoTouch issue - https://github.com/dolbz/Newtonsoft.Json/commit/a24e3062846b30ee505f3271ac08862bb471b822\n            return token == null ? default : Extensions.Convert<JToken, T>(token);\n        }\n\n        /// <summary>","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L337-L373","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check token.Type (or token is JObject / is JArray) before indexing.","Use token.Value<T>(key) which safely returns default for absent/leaf cases.","Validate the JSON shape against a schema before generic navigation."],"exampleFix":"// before\nJToken node = obj[\"data\"];        // actually a scalar in the data\nvar inner = node[\"id\"];           // throws InvalidOperationException: Cannot access child value on JValue\n\n// after\nif (node is JObject objNode)\n{\n    var inner = objNode[\"id\"];\n}","handlingStrategy":"type-guard","validationCode":"if (node is JObject objNode) { var inner = objNode[key]; }","typeGuard":"static bool IsIndexableContainer(JToken t) => t is JObject or JArray;","tryCatchPattern":null,"preventionTips":["Check node is JObject/JArray before using the indexer.","Use node.Value<T>(key) which returns default safely.","Validate JSON shape before generic keyed navigation."],"tags":["json","linq-to-json","jtoken","jvalue","index"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}