JamesNK/Newtonsoft.Json · error · ArgumentException
Can not convert {0} to Boolean.
Error message
Can not convert {0} to Boolean. What it means
Thrown by the explicit (non-nullable) cast operator bool(JToken) at JToken.cs:564. The operator unwraps the token to a JValue and validates its JTokenType against BooleanTypes = {Integer, Float, String, Comment, Raw, Boolean}. If the token is not a JValue at all (e.g. a JObject/JArray) or its type is outside that set (Null, Undefined, Date, Guid, Uri, TimeSpan, Bytes, Object, Array), it raises this ArgumentException.
Source
Thrown at Src/Newtonsoft.Json/Linq/JToken.cs:564
}
private static bool ValidateToken(JToken o, JTokenType[] validTypes, bool nullable)
{
return (Array.IndexOf(validTypes, o.Type) != -1) || (nullable && (o.Type == JTokenType.Null || o.Type == JTokenType.Undefined));
}
#region Cast from operators
/// <summary>
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.Boolean"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator bool(JToken value)
{
JValue? v = EnsureValue(value);
if (v == null || !ValidateToken(v, BooleanTypes, false))
{
throw new ArgumentException("Can not convert {0} to Boolean.".FormatWith(CultureInfo.InvariantCulture, GetType(value)));
}
#if HAVE_BIG_INTEGER
if (v.Value is BigInteger integer)
{
return Convert.ToBoolean((int)integer);
}
#endif
return Convert.ToBoolean(v.Value, CultureInfo.InvariantCulture);
}
#if HAVE_DATE_TIME_OFFSET
/// <summary>
/// Performs an explicit conversion from <see cref="Newtonsoft.Json.Linq.JToken"/> to <see cref="System.DateTimeOffset"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>View on GitHub (pinned to 4f73e74372)
Solutions
- Use the nullable cast (bool?)token and coalesce: ((bool?)token) ?? defaultValue — JSON null then yields null instead of throwing.
- Check token.Type == JTokenType.Boolean (or is within Integer/Float/String) before the non-nullable cast.
- Use token.Value<bool?>() which tolerates null/missing tokens.
- Deserialize into a strongly-typed POCO with a nullable bool property instead of manual casts.
Example fix
// before bool active = (bool)token["active"]; // throws when field is null // after bool active = (bool?)token["active"] ?? false;
Defensive patterns
Strategy: type-guard
Validate before calling
// Verify the token is a boolean-compatible scalar before the non-nullable cast
static bool CanToBool(JToken? t) =>
t is JValue v && (v.Type == JTokenType.Boolean || v.Type == JTokenType.Integer
|| v.Type == JTokenType.Float || v.Type == JTokenType.String); Type guard
static bool? SafeBool(JToken? t) => t switch {
JValue v when v.Type is JTokenType.Boolean or JTokenType.Integer
or JTokenType.Float or JTokenType.String => (bool?)t,
JValue v when v.Type is JTokenType.Null or JTokenType.Undefined => null,
_ => null
}; Try / catch
try
{
bool b = (bool)token;
}
catch (ArgumentException ex) when (ex.Message.StartsWith("Can not convert"))
{
// token type is not boolean-compatible; fall back to default
} Prevention
- Use the nullable cast (bool?)token for optional/nullable boolean fields.
- Check token.Type before applying the non-nullable bool cast.
- Prefer token.Value<bool?>() which tolerates missing/null tokens.
- Deserialize into a nullable bool POCO property for schema-driven safety.
When it happens
Trigger: Casting a JSON null (JTokenType.Null) to (bool)token; casting a JObject/JArray/JConstructor to bool; casting a Date/Guid/Uri/TimeSpan JValue to bool; calling (bool)token where token.Type is Object or Array.
Common situations: A JSON schema change that made a previously-boolean field null or an object; consuming an API that omits the field (parsed as null); treating a nested object/array as a scalar boolean.
Related errors
- Can not convert {0} to DateTimeOffset.
- Can not convert {0} to Int64.
- Can not convert {0} to DateTime.
- Can not convert {0} to Decimal.
- Can not convert {0} to Double.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/48c23436b59595d1.
Report an issue: GitHub.