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
- Inspect the numeric token reported (resstr in the message) to identify the structural problem.
- Ensure the data source emits numbers in standard JSON format (no thousands separators, single decimal point, standard exponent notation).
- 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.
- 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
- Ensure data sources serialize numbers using standard JSON numeric format.
- Avoid locale-specific number formatting (commas as decimal separators).
- Validate numeric fields with a regex before parsing if data quality is uncertain.
- Use InvariantCulture when converting numbers on the producer side.
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
- Invalid unicode escape char near {}
- Invalid escape char '{}' near {}
- End of json while parsing while parsing string near {}
- Invalid token at {}
- Tried to read non-string json value as string
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/691c112ea4624816.
Report an issue: GitHub.