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

  1. Check IsBool() before calling AsBool(), or use AsBool(true) to get false on mismatch.
  2. If the source is 0/1 or a string, read it as number/string and convert explicitly.
  3. 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

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


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/842479bcbc646d1f. Report an issue: GitHub.