Unity-Technologies/UnityCsReference · error · JSONParseException

Key not string type at {}

Error message

Key not string type at {}

What it means

ParseDict parses each key via ParseValue and then asserts key.IsString(); JSON requires object keys to be strings, so a key that parsed as a number, bool, null, object, or array triggers JSONParseException ("Key not string type at <pos>"). This catches malformed objects like {1: "x"} or {true: 1}.

Source

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

                {
                    Next();
                    SkipWs();
                }
            }
            Next();
            return new JSONValue(arr);
        }

        private JSONValue ParseDict()
        {
            Next();
            SkipWs();
            Dictionary<string, JSONValue> dict = new Dictionary<string, JSONValue>();
            while (cur != '}')
            {
                JSONValue key = ParseValue();
                if (!key.IsString())
                    throw new JSONParseException("Key not string type at " + PosMsg());
                SkipWs();
                if (cur != ':')
                    throw new JSONParseException("Missing dict entry delimiter ':' at " + PosMsg());
                Next();
                dict.Add(key.AsString(), ParseValue());
                SkipWs();
                if (cur == ',')
                {
                    Next();
                    SkipWs();
                }
            }
            Next();
            return new JSONValue(dict);
        }

        static readonly char[] endcodes = { '\\', '"' };

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Quote all object keys ("id": ... not id: ...).
  2. Build JSON with a proper serializer rather than string concatenation.
  3. Validate/round-trip the JSON through a strict parser during development.

Example fix

// before (malformed)
// { 1: "x" }

// after
// { "1": "x" }
Defensive patterns

Strategy: validation

Validate before calling

// Validate that object keys are quoted strings before parsing.
// Simplest robust approach: build JSON with a serializer that always quotes keys.

Try / catch

try { v = JSON.Parse(json); }
catch (JSONParseException ex) { /* ex.Message gives position of the bad key */ throw; }

Prevention

When it happens

Trigger: Hand-written or generated JSON with an unquoted numeric/boolean key; another serializer that emits non-string keys; a parser bug allowing an object as a key.

Common situations: Manually constructed JSON strings; concatenation building keys from variables without quoting; schema produced by a non-strict serializer.

Related errors


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