{"record":{"id":"7141f5120c5bf967","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-guid","errorCode":null,"errorMessage":"Can not convert {0} to Guid.","messagePattern":"Can not convert (.+?) to Guid\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1427,"sourceCode":"            if (v.Value is byte[] bytes)\n            {\n                return bytes;\n            }\n\n            throw new ArgumentException(\"Can not convert {0} to byte array.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Guid\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator Guid(JToken value)\n        {\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, GuidTypes, false))\n            {\n                throw new ArgumentException(\"Can not convert {0} to Guid.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n            if (v.Value is byte[] bytes)\n            {\n                return new Guid(bytes);\n            }\n\n            return (v.Value is Guid guid) ? guid : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Nullable{T}\"/> of <see cref=\"Guid\"/> .\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator Guid?(JToken? value)\n        {\n            if (value == null)","sourceCodeStart":1409,"sourceCodeEnd":1445,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1409-L1445","documentation":"Thrown by the explicit cast operator 'operator Guid(JToken)' (NON-nullable) when the token is not a Guid-compatible JValue. ArgumentException {0} is the resolved JTokenType; allowed types are GuidTypes ({String, Comment, Raw, Guid, Bytes}). Non-nullable overload, so a JSON null token ALSO throws. The library only accepts a GUID string, a Guid, or a 16-byte array; numeric/date/object tokens are rejected.","triggerScenarios":"Calling (Guid)token[\"id\"] when the JValue Type is Integer, Float, Boolean, Date, Uri, TimeSpan, Null, Object, or Array, or the node is a JObject/JArray (EnsureValue null). A malformed GUID STRING passes ValidateToken then throws FormatException from new Guid(string), not this error.","commonSituations":"Id field changed from GUID to integer or to a Mongo ObjectId hex string of wrong length; null GUID in some records cast to non-nullable Guid; mis-indexed node returning an object.","solutions":["Use the Nullable overload (Guid?)token[\"id\"] so JSON null returns null.","Guard token[\"id\"]?.Type is String or Guid before casting, and use Guid.TryParse on the string value.","If ids are now integers or ObjectIds, change the consuming type and parse accordingly.","Deserialize into a Guid? POCO property to absorb nulls cleanly."],"exampleFix":"// before\nGuid id = (Guid)payload[\"id\"]; // throws on JSON null or integer id\n\n// after\nGuid id = Guid.TryParse((string)payload[\"id\"]!, out var g) ? g : Guid.Empty;","handlingStrategy":"validation","validationCode":"Guid id = Guid.TryParse((string?)payload[\"id\"], out var g) ? g : Guid.Empty;","typeGuard":"static bool IsGuidLike(JToken? t) =>\n    t is JValue v && (v.Type == JTokenType.Guid || v.Type == JTokenType.String || v.Type == JTokenType.Bytes);","tryCatchPattern":"Guid id;\ntry { id = (Guid)payload[\"id\"]; }\ncatch (ArgumentException) { id = Guid.Empty; }","preventionTips":["Use the Nullable overload (Guid?) for fields that may be JSON null.","Always Guid.TryParse string nodes instead of blind casts (avoids FormatException too).","If ids became integers/ObjectIds, change the consuming type at the boundary."],"tags":["json","linq-to-json","type-cast","jtoken","guid","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}