{"record":{"id":"876af9b53b1ef172","repo":"JamesNK/Newtonsoft.Json","slug":"source-value-must-be-a-jtoken","errorCode":null,"errorMessage":"Source value must be a JToken.","messagePattern":"Source value must be a JToken\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/Extensions.cs","lineNumber":178,"sourceCode":"        public static U? Value<U>(this IEnumerable<JToken> value)\n        {\n            return value.Value<JToken, U>();\n        }\n\n        /// <summary>\n        /// Converts the value.\n        /// </summary>\n        /// <typeparam name=\"T\">The source collection type.</typeparam>\n        /// <typeparam name=\"U\">The type to convert the value to.</typeparam>\n        /// <param name=\"value\">A <see cref=\"JToken\"/> cast as a <see cref=\"IEnumerable{T}\"/> of <see cref=\"JToken\"/>.</param>\n        /// <returns>A converted value.</returns>\n        public static U? Value<T, U>(this IEnumerable<T> value) where T : JToken\n        {\n            ValidationUtils.ArgumentNotNull(value, nameof(value));\n\n            if (!(value is JToken token))\n            {\n                throw new ArgumentException(\"Source value must be a JToken.\");\n            }\n\n            return token.Convert<JToken, U>();\n        }\n\n        internal static IEnumerable<U?> Values<T, U>(this IEnumerable<T> source, object? key) where T : JToken\n        {\n            ValidationUtils.ArgumentNotNull(source, nameof(source));\n\n            if (key == null)\n            {\n                foreach (T token in source)\n                {\n                    if (token is JValue value)\n                    {\n                        yield return Convert<JValue, U>(value);\n                    }\n                    else","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/Extensions.cs#L160-L196","documentation":"Thrown by the LINQ-to-JSON Value<T,U>(this IEnumerable<T>) extension when the source sequence is not itself a JToken. The extension accepts IEnumerable<T> (where T:JToken) for ergonomics (so a single JToken can be treated as a one-element sequence), but the conversion requires the actual instance to be a JToken; a collection type like List<JToken> or JArray satisfies the parameter type but fails this guard with ArgumentException.","triggerScenarios":"Calling .Value<object>() / .Value<int>() on a List<JToken>, an array of JToken, or any IEnumerable<JToken> that is not itself a JToken; passing a LINQ Where/Select result (which is a sequence, not a token) to Value<T,U>.","commonSituations":"Confusing the single-token Value<T,U> extension (on IEnumerable<T> narrowed to a JToken) with the Values<T> extension meant for collections; chaining .Children().Value<...>() where Children returns a sequence.","solutions":["Call Value<T,U> only on a single JToken instance (e.g. a JValue/JProperty/JToken).","For sequences use .Values<T>() / .Values<T,U>(key) which enumerates each token.","Select a single token first: token.Children().First().Value<int>() rather than token.Children().Value<int>()."],"exampleFix":"// before\nint v = tokens.Value<int>(); // tokens is List<JToken>\n\n// after\nIEnumerable<int> v = tokens.Values<int>();","handlingStrategy":"type-guard","validationCode":"if (source is JToken token)\n{\n    var v = token.Value<int>();\n}","typeGuard":"static bool IsSingleToken<T>(IEnumerable<T> source) where T : JToken\n    => source is JToken;","tryCatchPattern":null,"preventionTips":["Use Value<T,U> only on a single JToken; use Values<T> on sequences.","Select a single token (.First() / SelectToken) before calling Value.","Distinguish single-token vs collection extensions when reading LINQ-to-JSON code."],"tags":["linq-to-json","type-mismatch","extension-method"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}