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
- 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.
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
- 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.
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
- Tried to read non-string json value as string
- Tried to read non-float json value as float
- Tried to read non-bool json value as bool
- Tried to read {} json value as list
- Cannot serialize json value of unknown type
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/b63d27f1e6637ee7.
Report an issue: GitHub.