{"record":{"id":"0a907f3050aade0f","repo":"Unity-Technologies/UnityCsReference","slug":"tried-to-read-json-value-as-list","errorCode":null,"errorMessage":"Tried to read {} json value as list","messagePattern":"Tried to read (.+?) json value as list","errorType":"exception","errorClass":"JSONTypeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetStore/Json.cs","lineNumber":121,"sourceCode":"        {\n            if (data is bool)\n                return (bool)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read non-bool json value as bool\");\n            return false;\n        }\n\n        public bool AsBool()\n        {\n            return AsBool(false);\n        }\n\n        public List<JSONValue> AsList(bool nothrow)\n        {\n            if (data is List<JSONValue>)\n                return (List<JSONValue>)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read \" + data.GetType().Name + \" json value as list\");\n            return null;\n        }\n\n        public List<JSONValue> AsList()\n        {\n            return AsList(false);\n        }\n\n        public Dictionary<string, JSONValue> AsDict(bool nothrow)\n        {\n            if (data is Dictionary<string, JSONValue>)\n                return (Dictionary<string, JSONValue>)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read non-dictionary json value as dictionary\");\n            return null;\n        }\n\n        public Dictionary<string, JSONValue> AsDict()","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetStore/Json.cs#L103-L139","documentation":"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.","triggerScenarios":"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.","commonSituations":"API collapses a one-element array to a single object; missing field serialized as null; field renamed/repurposed.","solutions":["Check IsList() before calling AsList(), or use AsList(true) which returns null on mismatch.","Handle the single-object-vs-array variants explicitly by testing the wrapped type.","Confirm the field is present and is an array in the schema before parsing."],"exampleFix":"// before\nforeach (var item in node[\"items\"].AsList()) { ... } // throws if items is an object\n\n// after\nvar items = node[\"items\"].AsList(true) ?? new List<JSONValue>();\n// or, when a single object should be treated as a 1-element list:\nif (!node[\"items\"].IsList() && node[\"items\"].IsDict())\n    items = new List<JSONValue> { node[\"items\"] };","handlingStrategy":"type-guard","validationCode":"List<JSONValue> ReadList(JSONValue v) {\n    var list = v.AsList(true);\n    if (list != null) return list;\n    if (v.IsDict()) return new List<JSONValue> { v }; // tolerate single-object form\n    return new List<JSONValue>();\n}","typeGuard":"static List<JSONValue> AsListOrEmpty(JSONValue v) => v.AsList(true) ?? new List<JSONValue>();","tryCatchPattern":"List<JSONValue> arr;\ntry { arr = v.AsList(); } catch (JSONTypeException) { arr = new List<JSONValue>(); }","preventionTips":["Handle the single-object-vs-array variants explicitly for fields known to collapse.","Use AsList(true) and null-check instead of letting the throw propagate.","Confirm array fields are present and typed as arrays in the schema."],"tags":["json","type-mismatch","asset-store","unity"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}