{"record":{"id":"ca0aaa68f0233192","repo":"JamesNK/Newtonsoft.Json","slug":"can-not-convert-0-to-uri","errorCode":null,"errorMessage":"Can not convert {0} to Uri.","messagePattern":"Can not convert (.+?) to Uri\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Linq/JToken.cs","lineNumber":1526,"sourceCode":"            return (v.Value is TimeSpan span) ? span : ConvertUtils.ParseTimeSpan(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);\n        }\n\n        /// <summary>\n        /// Performs an explicit conversion from <see cref=\"JToken\"/> to <see cref=\"Uri\"/>.\n        /// </summary>\n        /// <param name=\"value\">The value.</param>\n        /// <returns>The result of the conversion.</returns>\n        public static explicit operator Uri?(JToken? value)\n        {\n            if (value == null)\n            {\n                return null;\n            }\n\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, UriTypes, true))\n            {\n                throw new ArgumentException(\"Can not convert {0} to Uri.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }\n\n            if (v.Value == null)\n            {\n                return null;\n            }\n\n            return (v.Value is Uri uri) ? uri : new Uri(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);\n        }\n\n#if HAVE_BIG_INTEGER\n        private static BigInteger ToBigInteger(JToken value)\n        {\n            JValue? v = EnsureValue(value);\n            if (v == null || !ValidateToken(v, BigIntegerTypes, false))\n            {\n                throw new ArgumentException(\"Can not convert {0} to BigInteger.\".FormatWith(CultureInfo.InvariantCulture, GetType(value)));\n            }","sourceCodeStart":1508,"sourceCodeEnd":1544,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Linq/JToken.cs#L1508-L1544","documentation":"Thrown by the explicit cast operator 'operator Uri?(JToken?)' (Nullable<Uri>) when the token is not a Uri-compatible JValue. ArgumentException {0} is the resolved JTokenType; allowed types are UriTypes ({String, Comment, Raw, Uri}). Nullable overload, so null/JSON-null return null. Throw means a structurally incompatible shape such as Integer, Float, Boolean, Date, Guid, Uri-typed-wrapped-but-mismatched, Object, or Array.","triggerScenarios":"Calling (Uri)token[\"endpoint\"] where the resolved JTokenType is Integer, Float, Boolean, Date, Guid, Bytes, TimeSpan, Object, or Array, or the node is a JObject/JArray/JConstructor. A malformed URI STRING passes ValidateToken then throws UriFormatException from new Uri(string), not this error.","commonSituations":"Endpoint field emitted as an integer port or an object { \"host\":..., \"port\":... } instead of a URL string; null URL in some records; schema change from URL string to a structured object.","solutions":["Guard token[\"endpoint\"]?.Type is String or Uri before casting.","Use Uri.TryCreate on the string value: Uri.TryCreate((string)token[\"endpoint\"], UriKind.Absolute, out var u).","If the field became a structured object, reconstruct the URL string from its parts before parsing.","Prefer token.Value<Uri>(\"endpoint\") which returns null on incompatible shapes."],"exampleFix":"// before\nUri endpoint = (Uri)payload[\"endpoint\"]; // throws when endpoint is an object/int\n\n// after\nUri endpoint = Uri.TryCreate((string?)payload[\"endpoint\"], UriKind.Absolute, out var u) ? u : null;","handlingStrategy":"validation","validationCode":"Uri? endpoint = Uri.TryCreate((string?)payload[\"endpoint\"], UriKind.Absolute, out var u) ? u : null;","typeGuard":"static bool IsUriLike(JToken? t) =>\n    t is JValue v && (v.Type == JTokenType.Uri || v.Type == JTokenType.String);","tryCatchPattern":"Uri? endpoint;\ntry { endpoint = (Uri)payload[\"endpoint\"]; }\ncatch (ArgumentException) { endpoint = null; }","preventionTips":["Uri.TryCreate string nodes to also catch malformed URIs (UriFormatException).","Reconstruct URL strings from structured objects before parsing.","Prefer token.Value<Uri>(\"endpoint\") which returns null on incompatible shapes."],"tags":["json","linq-to-json","type-cast","jtoken","uri","newtonsoft-json"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}