Unity-Technologies/UnityCsReference · error · JSONTypeException

Tried to read non-float json value as float

Error message

Tried to read non-float json value as float

What it means

JSONValue.AsFloat(bool nothrow) returns the wrapped value only when it is a float; otherwise (with nothrow false, used by the parameterless AsFloat()) it throws JSONTypeException. Because the library stores numbers specifically as float, an integer-looking JSON number that was parsed as float will work, but a string or bool will not, and there is no implicit parsing of numeric strings.

Source

Thrown at Editor/Mono/AssetStore/Json.cs:93

        {
            if (data is string)
                return (string)data;
            if (!nothrow)
                throw new JSONTypeException("Tried to read non-string json value as string");
            return "";
        }

        public string AsString()
        {
            return AsString(false);
        }

        public float AsFloat(bool nothrow)
        {
            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()

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check IsFloat() (or read via AsFloat(true) which yields 0.0f) before committing to the typed read.
  2. If the source is a numeric string, call AsString(true) then float.TryParse it.
  3. Normalize/validate the response shape before consumption.

Example fix

// before
float v = node["price"].AsFloat(); // throws if "price" is "9.99"

// after
float v;
if (node["price"].IsString())
    float.TryParse(node["price"].AsString(),
        System.Globalization.NumberStyles.Float,
        System.Globalization.CultureInfo.InvariantCulture, out v);
else
    v = node["price"].AsFloat(true);
Defensive patterns

Strategy: type-guard

Validate before calling

float ReadFloat(JSONValue v) {
    if (v.IsFloat()) return v.AsFloat();
    if (v.IsString() && float.TryParse(v.AsString(), System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var f)) return f;
    return 0f;
}

Type guard

static float AsFloatOr(JSONValue v, float fallback) => v.IsFloat() ? v.AsFloat() : fallback;

Try / catch

float f;
try { f = v.AsFloat(); } catch (JSONTypeException) { f = 0f; }

Prevention

When it happens

Trigger: Calling AsFloat() on a value that is a string (e.g. "1.5"), bool, object, or array; reading a numeric field that the server sent as a quoted string.

Common situations: API returns numbers as strings; mixed-schema responses; field repurposed from number to string across versions.

Related errors


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