Unity-Technologies/UnityCsReference · error · JSONTypeException

Tried to read non-string json value as string

Error message

Tried to read non-string json value as string

What it means

Editor/Mono/AssetStore/Json.cs implements a small dynamically-typed JSON model: a JSONValue wraps an object that may be a string, float, bool, List<JSONValue>, or Dictionary. AsString(bool nothrow) returns the value only if it is actually a string; otherwise, when nothrow is false (the default path used by AsString()), it throws JSONTypeException. The library does not coerce numbers or bools to strings, so any schema mismatch surfaces as this exception.

Source

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

            return new JSONValue(s);
        }

        public static implicit operator JSONValue(int s)
        {
            return new JSONValue((float)s);
        }

        public object AsObject()
        {
            return data;
        }

        public string AsString(bool nothrow)
        {
            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()

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Use the nothrow overload first: string s = value.AsString(true); which returns "" instead of throwing.
  2. Check value.IsString() before calling AsString() and branch on the actual type.
  3. If the value is a number, read it via AsFloat()/AsInt-style accessors and convert with ToString().
  4. Validate the response schema (e.g. log value types during development) so mismatches are caught early.

Example fix

// before
string id = node["id"].AsString(); // throws if id is numeric

// after
string id = node["id"].IsString()
    ? node["id"].AsString()
    : node["id"].AsFloat(true).ToString(System.Globalization.CultureInfo.InvariantCulture);
Defensive patterns

Strategy: type-guard

Validate before calling

string ReadString(JSONValue v) => v.IsString() ? v.AsString() : v.AsString(true);

Type guard

static string AsStringOr(JSONValue v, string fallback) => v.IsString() ? v.AsString() : fallback;

Try / catch

string s;
try { s = v.AsString(); } catch (JSONTypeException) { s = v.AsFloat(true).ToString(System.Globalization.CultureInfo.InvariantCulture); }

Prevention

When it happens

Trigger: Calling json.AsString() on a value that is a number, bool, object, array, or null (e.g. reading an id field that the server serialized as a numeric JSON value); chaining .AsString() on a dictionary element accessed by key when the key holds a non-string.

Common situations: Asset Store / API response changed a field from string to number (or vice versa); untested response shapes; numeric IDs returned as JSON numbers while code treats them as strings.

Related errors


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