Unity-Technologies/UnityCsReference · error · JSONTypeException

Tried to read non-dictionary json value as dictionary

Error message

Tried to read non-dictionary json value as dictionary

What it means

JSONValue.AsDict(bool nothrow) returns the value only when it is a Dictionary<string,JSONValue>; the parameterless AsDict() throws JSONTypeException via the nothrow=false path. This is the mirror of AsList: it fires when code treats an array or scalar as a keyed object, e.g. indexing by string a value that the JSON modeled as a list.

Source

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

        {
            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()
        {
            return AsList(false);
        }

        public Dictionary<string, JSONValue> AsDict(bool nothrow)
        {
            if (data is Dictionary<string, JSONValue>)
                return (Dictionary<string, JSONValue>)data;
            if (!nothrow)
                throw new JSONTypeException("Tried to read non-dictionary json value as dictionary");
            return null;
        }

        public Dictionary<string, JSONValue> AsDict()
        {
            return AsDict(false);
        }

        public static JSONValue NewString(string val)
        {
            return new JSONValue(val);
        }

        public static JSONValue NewFloat(float val)
        {
            return new JSONValue(val);
        }

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Check IsDict() before calling AsDict(), or use AsDict(true) to get null on mismatch.
  2. Branch on the actual type when the field is polymorphic.
  3. Validate the response schema up front.

Example fix

// before
var props = node["props"].AsDict(); // throws if props is an array

// after
var props = node["props"].AsDict(true);
if (props == null) props = new Dictionary<string, JSONValue>();
Defensive patterns

Strategy: type-guard

Validate before calling

Dictionary<string, JSONValue> ReadDict(JSONValue v) => v.AsDict(true) ?? new Dictionary<string, JSONValue>();

Type guard

static Dictionary<string, JSONValue> AsDictOrEmpty(JSONValue v) => v.AsDict(true) ?? new Dictionary<string, JSONValue>();

Try / catch

Dictionary<string, JSONValue> d;
try { d = v.AsDict(); } catch (JSONTypeException) { d = new Dictionary<string, JSONValue>(); }

Prevention

When it happens

Trigger: Calling AsDict() on a JSON array, string, number, or bool; indexing node["key"] style access depends on the dictionary shape but the explicit AsDict() read fails when the value is not an object.

Common situations: Field sometimes an object, sometimes an array (e.g. a map collapsed to a list); missing/null field; schema drift.

Related errors


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