Unity-Technologies/UnityCsReference · error · JSONParseException

Cannot parse json value starting with

Error message

Cannot parse json value starting with '{}' at {}

What it means

ParseValue dispatches on the current character: '{' for object, '[' for array, '"' for string, '-' or digit for number, 't'/'f'/'n' for true/false/null. The default branch throws JSONParseException ("Cannot parse json value starting with '<first 5 chars>' at <pos>") for any other leading character. The placeholder shows the actual offending prefix so the bad byte is visible.

Solutions

  1. Inspect the first characters of the input (the message already shows the first 5) to identify the stray byte/markup.
  2. Strip a leading BOM and surrounding whitespace before parsing.
  3. Verify the response Content-Type is JSON and that any compression was decoded.
  4. Catch JSONParseException and surface the endpoint/length so misrouted responses are obvious.

Example fix

// before
var v = JSON.Parse(responseBody); // body starts with '<' (HTML)

// after
string body = responseBody.TrimStart('\uFEFF').Trim();
if (body.StartsWith("<"))
    throw new InvalidOperationException("Expected JSON, got markup: " + body.Substring(0, Math.Min(80, body.Length)));
var v = JSON.Parse(body);
Defensive patterns

Strategy: validation

Validate before calling

string CleanJson(string raw) {
    var s = raw.TrimStart('\uFEFF').Trim();
    if (s.Length == 0 || (s[0] != '{' && s[0] != '[' && s[0] != '"' && s[0] != '-' && !char.IsDigit(s[0]) && s[0] != 't' && s[0] != 'f' && s[0] != 'n'))
        throw new InvalidOperationException("Input does not start with a JSON value: " + s.Substring(0, System.Math.Min(40, s.Length)));
    return s;
}

Try / catch

try { v = JSON.Parse(json); }
catch (JSONParseException ex) {
    // ex.Message shows the first 5 chars; log endpoint + content-type to spot HTML/BOM
    throw;
}

Prevention

When it happens

Trigger: Leading byte-order mark (BOM), HTML/markup returned instead of JSON (e.g. '<' from an error page), unstripped leading whitespace that was not consumed, control characters, or an encoding mismatch producing unexpected bytes.

Common situations: Server returns an HTML error page with 200 status; UTF-8 BOM not stripped; gzip body not decompressed; binary garbage from a wrong endpoint.

Related errors


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

Appendix: source

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

                    return ParseString();
                case '-':
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    return ParseNumber();
                case 't':
                case 'f':
                case 'n':
                    return ParseConstant();
                default:
                    throw new JSONParseException("Cannot parse json value starting with '" + json.Substring(idx, 5) + "' at " + PosMsg());
            }
        }

        private JSONValue ParseArray()
        {
            Next();
            SkipWs();
            List<JSONValue> arr = new List<JSONValue>();
            while (cur != ']')
            {
                arr.Add(ParseValue());
                SkipWs();
                if (cur == ',')
                {
                    Next();
                    SkipWs();
                }
            }

View on GitHub (pinned to 225b0fbdb5)