{"record":{"id":"e72527df15503643","repo":"Unity-Technologies/UnityCsReference","slug":"tried-to-read-non-string-json-value-as-string","errorCode":null,"errorMessage":"Tried to read non-string json value as string","messagePattern":"Tried to read non-string json value as string","errorType":"exception","errorClass":"JSONTypeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetStore/Json.cs","lineNumber":79,"sourceCode":"            return new JSONValue(s);\n        }\n\n        public static implicit operator JSONValue(int s)\n        {\n            return new JSONValue((float)s);\n        }\n\n        public object AsObject()\n        {\n            return data;\n        }\n\n        public string AsString(bool nothrow)\n        {\n            if (data is string)\n                return (string)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read non-string json value as string\");\n            return \"\";\n        }\n\n        public string AsString()\n        {\n            return AsString(false);\n        }\n\n        public float AsFloat(bool nothrow)\n        {\n            if (data is float)\n                return (float)data;\n            if (!nothrow)\n                throw new JSONTypeException(\"Tried to read non-float json value as float\");\n            return 0.0f;\n        }\n\n        public float AsFloat()","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetStore/Json.cs#L61-L97","documentation":"Editor/Mono/AssetStore/Json.cs implements a small dynamically-typed JSON model: a JSONValue wraps an object that may be a string, float, bool, List<JSONValue>, or Dictionary. AsString(bool nothrow) returns the value only if it is actually a string; otherwise, when nothrow is false (the default path used by AsString()), it throws JSONTypeException. The library does not coerce numbers or bools to strings, so any schema mismatch surfaces as this exception.","triggerScenarios":"Calling json.AsString() on a value that is a number, bool, object, array, or null (e.g. reading an id field that the server serialized as a numeric JSON value); chaining .AsString() on a dictionary element accessed by key when the key holds a non-string.","commonSituations":"Asset Store / API response changed a field from string to number (or vice versa); untested response shapes; numeric IDs returned as JSON numbers while code treats them as strings.","solutions":["Use the nothrow overload first: string s = value.AsString(true); which returns \"\" instead of throwing.","Check value.IsString() before calling AsString() and branch on the actual type.","If the value is a number, read it via AsFloat()/AsInt-style accessors and convert with ToString().","Validate the response schema (e.g. log value types during development) so mismatches are caught early."],"exampleFix":"// before\nstring id = node[\"id\"].AsString(); // throws if id is numeric\n\n// after\nstring id = node[\"id\"].IsString()\n    ? node[\"id\"].AsString()\n    : node[\"id\"].AsFloat(true).ToString(System.Globalization.CultureInfo.InvariantCulture);","handlingStrategy":"type-guard","validationCode":"string ReadString(JSONValue v) => v.IsString() ? v.AsString() : v.AsString(true);","typeGuard":"static string AsStringOr(JSONValue v, string fallback) => v.IsString() ? v.AsString() : fallback;","tryCatchPattern":"string s;\ntry { s = v.AsString(); } catch (JSONTypeException) { s = v.AsFloat(true).ToString(System.Globalization.CultureInfo.InvariantCulture); }","preventionTips":["Check IsString() before AsString() for any field whose type is not guaranteed.","Use the nothrow overload (AsString(true)) when a missing/typed value is tolerable.","During development, log wrapped CLR types to catch schema drift early."],"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"}