Unity-Technologies/UnityCsReference · error · JSONParseException

Cannot convert string to float : '{}' at {}

Error message

Cannot convert string to float : '{}' at {}

What it means

Thrown by ParseNumber when the collected numeric character sequence cannot be converted to a float via Convert.ToSingle with InvariantCulture. This covers malformed numbers that passed the initial character-scanning loop but fail actual numeric conversion (e.g. stray characters, multiple decimal points, leading zeros issues).

Source

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

                    // throw new JSONParseException("Missing - or + in 'e' potent specifier at " + PosMsg());
                    resstr += cur;
                    Next();
                }
                while (cur >= '0' && cur <= '9')
                {
                    resstr += cur;
                    Next();
                }
            }

            try
            {
                float f = System.Convert.ToSingle(resstr, CultureInfo.InvariantCulture);
                return new JSONValue(f);
            }
            catch (Exception)
            {
                throw new JSONParseException("Cannot convert string to float : '" + resstr + "' at " + PosMsg());
            }
        }

        private JSONValue ParseConstant()
        {
            string c = "";
            c = "" + cur + Next() + Next() + Next();
            Next();
            if (c == "true")
            {
                return new JSONValue(true);
            }
            else if (c == "fals")
            {
                if (cur == 'e')
                {
                    Next();
                    return new JSONValue(false);

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. Inspect the numeric token reported (resstr in the message) to identify the structural problem.
  2. Ensure the data source emits numbers in standard JSON format (no thousands separators, single decimal point, standard exponent notation).
  3. If parsing user-supplied or third-party data, pre-validate numeric tokens with a regex like ^-?\d+(\.\d+)?([eE][+-]?\d+)?$ before passing to the parser.
  4. Re-fetch the data to rule out transient corruption.

Example fix

// before — malformed number in payload
string json = "{\"price\": 1.2.3}";
// after — valid number
string json = "{\"price\": 1.23}";
Defensive patterns

Strategy: validation

Validate before calling

// Pre-validate numeric tokens in JSON before parsing
using System.Text.RegularExpressions;
bool HasValidNumbers(string json)
{
    // Find all number-like tokens and validate format
    var matches = Regex.Matches(json, @"-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?");
    // Additionally, scan for malformed numbers like 1.2.3
    return !Regex.IsMatch(json, @"\d\.\d+\.\d");
}

Try / catch

try
{
    var result = JSONParser.SimpleParse(jsonString);
}
catch (JSONParseException ex) when (ex.Message.Contains("Cannot convert string to float"))
{
    Debug.LogError($"Malformed number in JSON: {ex.Message}");
}

Prevention

When it happens

Trigger: Parsing a JSON token that starts like a number (digit, minus, or decimal point) but is structurally invalid as a floating-point value — e.g. "1.2.3", "--5", ".". The scanner accumulates characters it considers numeric, but Convert.ToSingle rejects the result.

Common situations: Corrupted Asset Store JSON with garbled numeric fields, locale-specific number formatting leaking into JSON, data source bugs emitting malformed numeric values, proxy or encoding transformations mangling number strings.

Related errors


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