Unity-Technologies/UnityCsReference · error · JSONTypeException
Tried to read non-bool json value as bool
Error message
Tried to read non-bool json value as bool
What it means
JSONValue.AsBool(bool nothrow) returns the value only when it is a bool; the parameterless AsBool() uses nothrow=false and throws JSONTypeException on any other type. There is no coercion of 0/1, "true"/"false" strings, so a JSON field that uses 1/0 or strings for booleans will not satisfy the bool check.
Source
Thrown at Editor/Mono/AssetStore/Json.cs:107
{
if (data is float)
return (float)data;
if (!nothrow)
throw new JSONTypeException("Tried to read non-float json value as float");
return 0.0f;
}
public float AsFloat()
{
return AsFloat(false);
}
public bool AsBool(bool nothrow)
{
if (data is bool)
return (bool)data;
if (!nothrow)
throw new JSONTypeException("Tried to read non-bool json value as bool");
return false;
}
public bool AsBool()
{
return AsBool(false);
}
public List<JSONValue> AsList(bool nothrow)
{
if (data is List<JSONValue>)
return (List<JSONValue>)data;
if (!nothrow)
throw new JSONTypeException("Tried to read " + data.GetType().Name + " json value as list");
return null;
}
public List<JSONValue> AsList()View on GitHub (pinned to 225b0fbdb5)
Solutions
- Check IsBool() before calling AsBool(), or use AsBool(true) to get false on mismatch.
- If the source is 0/1 or a string, read it as number/string and convert explicitly.
- Validate the expected boolean encoding at the schema boundary.
Example fix
// before
bool enabled = node["enabled"].AsBool(); // throws if "enabled" is 1
// after
bool enabled = node["enabled"].IsBool()
? node["enabled"].AsBool()
: node["enabled"].AsFloat(true) != 0f; Defensive patterns
Strategy: type-guard
Validate before calling
bool ReadBool(JSONValue v) {
if (v.IsBool()) return v.AsBool();
if (v.IsFloat()) return v.AsFloat() != 0f;
if (v.IsString()) return v.AsString() == "true";
return false;
} Type guard
static bool AsBoolOr(JSONValue v, bool fallback) => v.IsBool() ? v.AsBool() : fallback;
Try / catch
bool b;
try { b = v.AsBool(); } catch (JSONTypeException) { b = false; } Prevention
- Normalize non-bool encodings (0/1, "true"/"false") at the schema boundary.
- Use IsBool() before AsBool() for fields that may drift.
- Document the canonical boolean encoding expected from each endpoint.
When it happens
Trigger: Calling AsBool() on a value that is a number (0/1), a string ("true"), object, or array; reading a flag field whose representation differs from a JSON boolean.
Common situations: API uses integers for boolean flags; strings "true"/"false"; field type changed across versions.
Related errors
- Tried to read non-string json value as string
- Tried to read non-float json value as float
- Tried to read {} json value as list
- Tried to read non-dictionary json value as dictionary
- Cannot serialize json value of unknown type
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/842479bcbc646d1f.
Report an issue: GitHub.