Unity-Technologies/UnityCsReference · error · JSONTypeException

Tried to read json value as list

Error message

Tried to read {} json value as list

What it means

JSONValue.AsList(bool nothrow) returns the value only when it is a List<JSONValue>; otherwise it builds a message of the form "Tried to read <ActualType> json value as list" using data.GetType().Name, so the placeholder in the message reflects the real wrapped CLR type (e.g. String, Dictionary`2, Single). The parameterless AsList() throws via the nothrow=false path. This fires whenever code indexes into something that the JSON modeled as an object or scalar rather than an array.

Solutions

  1. Check IsList() before calling AsList(), or use AsList(true) which returns null on mismatch.
  2. Handle the single-object-vs-array variants explicitly by testing the wrapped type.
  3. Confirm the field is present and is an array in the schema before parsing.

Example fix

// before
foreach (var item in node["items"].AsList()) { ... } // throws if items is an object

// after
var items = node["items"].AsList(true) ?? new List<JSONValue>();
// or, when a single object should be treated as a 1-element list:
if (!node["items"].IsList() && node["items"].IsDict())
    items = new List<JSONValue> { node["items"] };
Defensive patterns

Strategy: type-guard

Validate before calling

List<JSONValue> ReadList(JSONValue v) {
    var list = v.AsList(true);
    if (list != null) return list;
    if (v.IsDict()) return new List<JSONValue> { v }; // tolerate single-object form
    return new List<JSONValue>();
}

Type guard

static List<JSONValue> AsListOrEmpty(JSONValue v) => v.AsList(true) ?? new List<JSONValue>();

Try / catch

List<JSONValue> arr;
try { arr = v.AsList(); } catch (JSONTypeException) { arr = new List<JSONValue>(); }

Prevention

When it happens

Trigger: Calling AsList() on a JSON object or scalar (e.g. node["items"].AsList() when "items" is an object keyed by id, or when the server omitted the array and returned null); iterating a field that is sometimes an array and sometimes a single object.

Common situations: API collapses a one-element array to a single object; missing field serialized as null; field renamed/repurposed.

Related errors


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

Appendix: source

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

        {
            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()
        {
            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()

View on GitHub (pinned to 225b0fbdb5)