JamesNK/Newtonsoft.Json · error · ArgumentException
Can not convert {0} to Double.
Error message
Can not convert {0} to Double. What it means
Thrown by the explicit nullable cast operator double?(JToken?) at JToken.cs:764. With nullable:true, JSON null/undefined tokens pass and return null. The error fires when the token is not a JValue or its type is outside NumberTypes = {Integer, Float, String, Comment, Raw, Boolean} — e.g. Date, Guid, Uri, Bytes, Object, Array.
Source
Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:764
return (v.Value != null) ? (decimal?)Convert.ToDecimal(v.Value, CultureInfo.InvariantCulture) : null;
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="Nullable{T}"/> of <see cref="Double"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator double?(JToken? value)
{
if (value == null)
{
return null;
}
JValue? v = EnsureValue(value);
if (v == null || !ValidateToken(v, NumberTypes, true))
{
throw new ArgumentException("Can not convert {0} to Double.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
#if HAVE_BIG_INTEGER
if (v.Value is BigInteger integer)
{
return (double?)integer;
}
#endif
return (v.Value != null) ? (double?)Convert.ToDouble(v.Value, CultureInfo.InvariantCulture) : null;
}
/// <summary>
/// Performs an explicit conversion from <see cref="JToken"/> to <see cref="Nullable{T}"/> of <see cref="Char"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator char?(JToken? value)View on GitHub (pinned to 4f73e74372)
Solutions
- Guard with token.Type in {Integer, Float, String, Boolean} before the cast.
- Use token.Value<double?>() which returns default for incompatible tokens.
- Inspect the JSON to confirm a scalar number, not a container, is present.
- Deserialize into a nullable double POCO property.
Example fix
// before
double? v = (double?)token["value"]; // throws when value is a date
// after
double? v = (token["value"]?.Type is JTokenType.Integer or JTokenType.Float)
? (double?)token["value"]
: null; Defensive patterns
Strategy: type-guard
Validate before calling
// Verify the token is a number-compatible scalar (nullable cast tolerates JSON null)
static bool CanToNullableDouble(JToken? t) =>
t is not JValue v || v.Type is JTokenType.Integer or JTokenType.Float
or JTokenType.String or JTokenType.Boolean or JTokenType.Null or JTokenType.Undefined; Type guard
static double? SafeDouble(JToken? t) => t switch {
JValue v when v.Type is JTokenType.Integer or JTokenType.Float
or JTokenType.String or JTokenType.Boolean => (double?)t,
JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,
_ => null
}; Try / catch
try
{
double? d = (double?)token;
}
catch (ArgumentException ex) when (ex.Message.StartsWith("Can not convert"))
{
// token is a container or Date/Guid; default to null
} Prevention
- Check token.Type is Integer/Float before casting.
- Use token.Value<double?>() for tolerant default handling.
- Confirm the field is a numeric scalar, not a nested object/date.
- Deserialize into a nullable double POCO property.
When it happens
Trigger: Casting a Date/Guid/Uri JValue or a JObject/JArray to (double?)token; a measurement field whose Type is Date or Object. A JSON null returns null.
Common situations: A numeric measurement field came back as a nested object or date; a Guid/Uri value read as a double; a field re-typed by an upstream service.
Related errors
- Can not convert {0} to Boolean.
- Can not convert {0} to DateTimeOffset.
- Can not convert {0} to Int64.
- Can not convert {0} to DateTime.
- Can not convert {0} to Decimal.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/792ad89c44f6cdb3.
Report an issue: GitHub.