JamesNK/Newtonsoft.Json · error · ArgumentException
Can not convert {0} to Guid.
Error message
Can not convert {0} to Guid. What it means
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.
Source
Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:1427
if (v.Value is byte[] bytes)
{
return bytes;
}
throw new ArgumentException("Can not convert {0} to byte array.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="Guid"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator Guid(JToken value)
{
JValue? v = EnsureValue(value);
if (v == null || !ValidateToken(v, GuidTypes, false))
{
throw new ArgumentException("Can not convert {0} to Guid.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
if (v.Value is byte[] bytes)
{
return new Guid(bytes);
}
return (v.Value is Guid guid) ? guid : new Guid(Convert.ToString(v.Value, CultureInfo.InvariantCulture)!);
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="Nullable{T}"/> of <see cref="Guid"/> .
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator Guid?(JToken? value)
{
if (value == null)View on GitHub (pinned to 4f73e74372)
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.
Example fix
// before Guid id = (Guid)payload["id"]; // throws on JSON null or integer id // after Guid id = Guid.TryParse((string)payload["id"]!, out var g) ? g : Guid.Empty;
Defensive patterns
Strategy: validation
Validate before calling
Guid id = Guid.TryParse((string?)payload["id"], out var g) ? g : Guid.Empty;
Type guard
static bool IsGuidLike(JToken? t) =>
t is JValue v && (v.Type == JTokenType.Guid || v.Type == JTokenType.String || v.Type == JTokenType.Bytes); Try / catch
Guid id;
try { id = (Guid)payload["id"]; }
catch (ArgumentException) { id = Guid.Empty; } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Can not convert {0} to Single.
- Can not convert {0} to UInt32.
- Can not convert {0} to UInt64.
- Can not convert {0} to String.
- Can not convert {0} to byte array.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/7141f5120c5bf967.
Report an issue: GitHub.