{"record":{"id":"b63d27f1e6637ee7","repo":"Unity-Technologies/UnityCsReference","slug":"tried-to-read-non-dictionary-json-value-as-diction","errorCode":null,"errorMessage":"Tried to read non-dictionary json value as dictionary","messagePattern":"Tried to read non-dictionary json value as dictionary","errorType":"exception","errorClass":"JSONTypeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetStore/Json.cs","lineNumber":135,"sourceCode":"        {\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()\n        {\n            return AsDict(false);\n        }\n\n        public static JSONValue NewString(string val)\n        {\n            return new JSONValue(val);\n        }\n\n        public static JSONValue NewFloat(float val)\n        {\n            return new JSONValue(val);\n        }\n","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetStore/Json.cs#L117-L153","documentation":"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.","triggerScenarios":"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.","commonSituations":"Field sometimes an object, sometimes an array (e.g. a map collapsed to a list); missing/null field; schema drift.","solutions":["Check IsDict() before calling AsDict(), or use AsDict(true) to get null on mismatch.","Branch on the actual type when the field is polymorphic.","Validate the response schema up front."],"exampleFix":"// before\nvar props = node[\"props\"].AsDict(); // throws if props is an array\n\n// after\nvar props = node[\"props\"].AsDict(true);\nif (props == null) props = new Dictionary<string, JSONValue>();","handlingStrategy":"type-guard","validationCode":"Dictionary<string, JSONValue> ReadDict(JSONValue v) => v.AsDict(true) ?? new Dictionary<string, JSONValue>();","typeGuard":"static Dictionary<string, JSONValue> AsDictOrEmpty(JSONValue v) => v.AsDict(true) ?? new Dictionary<string, JSONValue>();","tryCatchPattern":"Dictionary<string, JSONValue> d;\ntry { d = v.AsDict(); } catch (JSONTypeException) { d = new Dictionary<string, JSONValue>(); }","preventionTips":["Branch on IsDict()/IsList() for polymorphic fields.","Use AsDict(true) and null-check rather than the throwing overload.","Validate object-vs-array shapes during development."],"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"}