Fix JSON Error: Invalid Number Format

JSON numbers look simple, but there are several formats that JavaScript accepts that JSON rejects — leading zeros, NaN, Infinity, hex, and more. Here's the complete guide.

JSONTech TeamMarch 1, 20255 min read

The Error Message

SyntaxError: Unexpected number in JSON at position 14
SyntaxError: Unexpected token 'N' in JSON at position 10
SyntaxError: Unexpected token 'I' in JSON at position 10
JSON.parse: unexpected non-digit in number at line 2 column 9

Number format errors are triggered when the parser finds something that looks like a number but doesn't conform to JSON's strict number rules.

JSON Number Rules

Valid JSON numbers must follow these rules:

  • Optional leading minus sign (-)
  • Integer part: either 0 or a digit 1-9 followed by more digits
  • Optional decimal part: a . followed by one or more digits
  • Optional exponent: e or E, optional + or -, followed by one or more digits

That's it. Everything else — hex, octal, leading plus, NaN, Infinity — is invalid.

Invalid Number Formats

1. Leading Zeros

In JSON, a number cannot start with 0 followed by another digit. The number 0 by itself is fine, and 0.5 is fine, but 012 is not. Some languages interpret leading zeros as octal (base 8), which leads to confusion — JSON avoids this entirely by banning them.

// ❌ Broken — leading zeros
{
  "zipCode": 01234,
  "port": 08080
}
// ✅ Fixed — remove leading zeros (or use strings for codes)
{
  "zipCode": "01234",
  "port": 8080
}

Note: zip codes and similar identifiers that start with zero should be stored as strings, not numbers.

2. NaN and Infinity

JavaScript has NaN, Infinity, and -Infinity as special number values. JSON.stringify() converts them to null, but if you try to put them in JSON manually, the parser rejects them.

// ❌ Broken — NaN and Infinity are not valid JSON
{
  "result": NaN,
  "max": Infinity,
  "min": -Infinity
}
// ✅ Fixed — use null or string representations
{
  "result": null,
  "max": null,
  "min": null
}

If you need to preserve the meaning, use strings or a sentinel value that your application understands:

{
  "result": "NaN",
  "max": 1.7976931348623157e+308,
  "min": -1.7976931348623157e+308
}

3. Leading Plus Sign

JSON allows a leading minus (-1) but not a leading plus (+1). Positive numbers are written without any sign.

// ❌ Broken — leading plus sign
{
  "offset": +5,
  "temperature": +23.4
}
// ✅ Fixed — remove the plus sign
{
  "offset": 5,
  "temperature": 23.4
}

4. Hexadecimal Numbers

JavaScript supports hex literals like 0xFF, but JSON only supports decimal numbers.

// ❌ Broken — hex literal
{
  "color": 0xFF5733,
  "mask": 0x00FF
}
// ✅ Fixed — convert to decimal or use strings
{
  "color": 16734003,
  "mask": 255
}

For color values, you're usually better off storing them as strings:

{
  "color": "#FF5733",
  "mask": "0x00FF"
}

5. Numbers With No Digits After the Decimal

A trailing decimal point with no following digit is invalid. So is a leading decimal with no digit before it.

// ❌ Broken
{
  "price": 10.,
  "discount": .5
}
// ✅ Fixed
{
  "price": 10.0,
  "discount": 0.5
}

How to Fix It Step by Step

  1. Find the problematic value. The error position points to the start of the invalid number.
  2. Check against the rules above. Is it hex? A leading zero? NaN? Identify which rule it breaks.
  3. Apply the right conversion:
    • Leading zeros → remove them or switch to a string
    • NaN/Infinity → use null or a string
    • Leading plus → drop the +
    • Hex → convert to decimal or a string
    • Bare decimal → add the missing digit (0.5 not .5)
  4. Validate.Run the corrected JSON through a parser to confirm it's accepted.

Prevention Tips

  • Always use JSON.stringify() to serialize numbers — it handles NaN, Infinity, and formatting automatically.
  • Store identifiers that happen to look like numbers (zip codes, phone numbers, IDs with leading zeros) as strings, not numbers.
  • Be especially careful with hex color values — store them as strings like "#FF5733" rather than numeric literals.
  • Validate JSON produced by any code path that constructs number values manually rather than through a serializer.

Fix it automatically: Paste your broken JSON into our JSON Repair tool — it handles this error and dozens more. Or validate your JSON first with our JSON Validator.

Related Tools