{"record":{"id":"7a24ca3db03ca9e7","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-string","errorCode":null,"errorMessage":"Can not convert {0} to String.","messagePattern":"Can not convert (.+?) to String\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1309,"sourceCode":"            return Convert.ToSingle(v.Value, CultureInfo.InvariantCulture);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"String\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator string?(JToken? value)\n        {\n            if (value == null)\n            {\n                return null;\n            }\n\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, StringTypes, true))\n            {\n                throw new ArgumentException(\"Can not convert {0} to String.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n            if (v.Value == null)\n            {\n                return null;\n            }\n\n            if (v.Value is byte[] bytes)\n            {\n                return Convert.ToBase64String(bytes);\n            }\n\n#if HAVE_BIG_INTEGER\n            if (v.Value is BigInteger integer)\n            {\n                return integer.ToString(CultureInfo.InvariantCulture);\n            }\n#endif","sourceCodeStart":1291,"sourceCodeEnd":1327,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1291-L1327","documentation":"Thrown by the explicit cast operator 'operator string?(JToken?)' when the token cannot be resolved to a scalar JValue. StringTypes is the broadest set ({Date, Integer, Float, String, Comment, Raw, Boolean, Bytes, Guid, TimeSpan, Uri}) and the overload is nullable (so JSON null returns null). Therefore this throw is ONLY reached when the token is not a JValue at all — i.e. a JObject, JArray, or JConstructor (EnsureValue returns null).","triggerScenarios":"Calling (string)token[\"name\"] where the node is a JSON object {...} or array [...] instead of a scalar, or the node is a JConstructor. Scalars of every kind (numbers, dates, GUIDs, null) convert fine and do not throw here.","commonSituations":"Indexing payload[\"name\"] and accidentally grabbing a child object (e.g. { \"first\": ..., \"last\": ... }); array-valued field where a string was expected; schema change that nested the scalar inside an object.","solutions":["Check token[\"name\"] is a JValue (or token[\"name\"]?.Type != JTokenType.Object/Array) before the string cast.","If the field is an object, navigate to the nested string scalar (e.g. payload[\"name\"][\"full\"]).","Use token[\"name\"]?.ToString() or token.Value<string>(\"name\") which return null instead of throwing for object/array nodes.","Re-examine the indexing path; this error almost always means you are one level too shallow or too deep."],"exampleFix":"// before\nstring name = (string)payload[\"name\"]; // throws when name is an object\n\n// after\nstring name = (payload[\"name\"] is JValue) ? (string)payload[\"name\"]! : null;","handlingStrategy":"type-guard","validationCode":"string? name = (payload[\"name\"] is JValue) ? (string)payload[\"name\"]! : null;","typeGuard":"static bool IsScalar(JToken? t) => t is JValue;","tryCatchPattern":"string? name;\ntry { name = (string)payload[\"name\"]; }\ncatch (ArgumentException) { name = null; }","preventionTips":["This throw means the node is an object/array, not a scalar — re-check your indexing depth.","Use token[\"name\"]?.ToString() or Value<string>(\"name\") to avoid the throw.","If the field is an object, navigate to the nested scalar before casting."],"tags":["json","linq-to-json","type-cast","jtoken","string","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}