Invalid number in JSON: why 01, .5, +1 and 0x1F fail and how to fix them
Updated
JSON numbers have one spelling. 01, .5, +1 and 0x1F are someone else’s. JavaScriptâs Number() reads all four; JSON accepts decimal digits, one optional minus, a dot with digits on both sides and an exponent, nothing more.
Numbers JavaScript reads and JSON rejects
Every value in this object is a number somewhere: a postal code with a leading zero, a fraction without its zero, an explicit plus, a hex mask, a trailing dot. None of them is a JSON number:
{"zip": 02134, "ratio": .5, "delta": +1, "mask": 0x1F, "total": 10.}The fixed version. The postal code becomes a string, because its leading zero is data; the rest are rewritten as plain decimals:
{"zip": "02134", "ratio": 0.5, "delta": 1, "mask": 31, "total": 10.0}The JSON number grammar (RFC 8259)
RFC 8259, section 6 is short and leaves no room: a number “is represented in base 10 using decimal digits”, “Leading zeros are not allowed”, and “A fraction part is a decimal point followed by one or more digits.” The grammar:
number = [ minus ] int [ frac ] [ exp ]
int = zero / ( digit1-9 *DIGIT )
frac = decimal-point 1*DIGIT
exp = e [ minus / plus ] 1*DIGITValid: 0, -0, 0.5, 10.0, 1e5, 1E-7, 1e+5, and integers of any length (12345678901234567890 is valid, though many parsers lose its last digits). Invalid: a plus in front, zeros in front of other digits, a dot without digits on either side, hex, octal, binary, 1_000, and the decimal comma.
The decimal comma deserves a warning. {"price": 1,5} fails, but not as a number: after 1, the parser expects the next key and finds 5. In an array [1,5] is perfectly valid and means two numbers, 1 and 5, so the mistake can pass every check.
Invalid number messages by parser
Each input sits in {"a":…}, so the bad number starts at character index 5. Messages come from running each parser on 2026-11-08; Firefox’s from the SpiderMonkey source. The table keeps one or two telling cases per parser rather than every combination.
| Parser | Input | Message |
|---|---|---|
| Chrome 156, Node 24–25 (V8) | 01 | Unexpected number in JSON at position 6 (line 1 column 7) |
| V8 | .5 | Unexpected token '.', "{"a":.5}" is not valid JSON |
| V8 | 1. | Unterminated fractional number in JSON at position 7 (line 1 column 8) |
| V8 | 1e | Exponent part is missing a number in JSON at position 7 (line 1 column 8) |
| V8 | 0x1F | Expected ',' or '}' after property value in JSON at position 6 (line 1 column 7) |
| Firefox (SpiderMonkey) | 01 | expected ',' or '}' after property value in object |
| Firefox | 1. | unterminated fractional number |
| Safari 27 (JavaScriptCore) | 01 | JSON Parse error: Expected '}' |
| Safari 27 | 1. | JSON Parse error: Invalid digits after decimal point |
| Python 3.9–3.14 | 01 | Expecting ',' delimiter: line 1 column 7 (char 6) |
| Python 3.9–3.14 | .5, +1 | Expecting value: line 1 column 6 (char 5) |
| Go 1.25–1.27 | 01 | invalid character '1' after object key:value pair |
| Go 1.25–1.26 | 1. | invalid character '}' after decimal point in numeric literal |
| Go 1.27 | 1., 1e | invalid character '}' in numeric literal |
| Java, Jackson 2.15–2.23 | 01 | Invalid numeric value: Leading zeroes not allowed |
| Jackson | 1. | Unexpected character ('}' (code 125)) in numeric value: Decimal point not followed by a digit |
| C#, System.Text.Json 10 | 01 | Invalid leading zero before '1'. |
| System.Text.Json | 0x1F | 'x' is an invalid end of a number. Expected a delimiter. |
| C#, Newtonsoft.Json 13 | 01, .5, 1., 0x1F | Accepted |
| PHP 8.3 | Any of them | Syntax error |
| Rust, serde_json 1.0 | 01 | invalid number at line 1 column 7 |
| jq 1.7.1 | 01, .5, +1, 1. | Accepted |
| jq 1.7.1 | 0x1F | Invalid numeric literal at line 1, column 10 |
| MySQL 8.4: JSON column | 1. | Invalid JSON text: "Miss fraction part in number." at position 7 |
| MySQL 8.4 | 1e | Invalid JSON text: "Miss exponent in number." at position 7 |
| JSON Statham Validator | Any of them | Invalid number, line 1, column 6, quoting the whole token |
A pattern worth knowing: for 01 and 0x1F many parsers don’t complain about the number at all. They read 0 as a complete number and then stumble over 1 or x, so the message talks about a missing comma. jq’s Invalid numeric literal is the opposite trap: jq says it for anything it can’t start a value with, including the < of an HTML page.
How to write numbers that JSON accepts
Serializers never write 01 or .5; the trouble starts when a number is pasted into JSON text as it was typed. Check the text against the grammar and quote it as a string when it doesn’t match. The snippets were run; the comments show their output.
JavaScript
const jsonNumber = /^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?$/;
function numberOrString(raw) {
return jsonNumber.test(raw) ? raw : JSON.stringify(raw);
}
numberOrString('0.5'); // 0.5
numberOrString('.5'); // ".5"
numberOrString('02134'); // "02134"Python
import json
import re
json_number = re.compile(r"-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?")
def number_or_string(raw):
return raw if json_number.fullmatch(raw) else json.dumps(raw)
print(number_or_string("1e3"), number_or_string("+1"), number_or_string("0x1F")) # 1e3 "+1" "0x1F"Go
import (
"encoding/json"
"regexp"
)
var jsonNumber = regexp.MustCompile(`^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?$`)
func numberOrString(raw string) string {
if jsonNumber.MatchString(raw) {
return raw
}
quoted, _ := json.Marshal(raw)
return string(quoted)
}Java (Jackson)
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.regex.Pattern;
static final Pattern JSON_NUMBER = Pattern.compile("-?(0|[1-9]\\d*)(\\.\\d+)?([eE][+-]?\\d+)?");
static String numberOrString(ObjectMapper mapper, String raw) throws JsonProcessingException {
return JSON_NUMBER.matcher(raw).matches() ? raw : mapper.writeValueAsString(raw);
}C#
using System.Text.Json;
using System.Text.RegularExpressions;
static string NumberOrString(string raw) =>
Regex.IsMatch(raw, @"^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?\z") ? raw : JsonSerializer.Serialize(raw);System.Text.Json escapes + in strings by default, so "+1" comes out as "\u002B1": the same string, written more cautiously.
PHP
function number_or_string(string $raw): string
{
return preg_match('/^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?\z/', $raw) === 1
? $raw
: json_encode($raw, JSON_THROW_ON_ERROR);
}To keep the value numeric instead, convert before writing: parseInt("1F", 16) in JavaScript, int("1F", 16) in Python, strconv.ParseInt in Go, Integer.parseInt("1F", 16) in Java, Convert.ToInt32("1F", 16) in C#, hexdec("1F") in PHP all give 31.
How to keep invalid numbers out of JSON
- Codes are strings. Postal codes, phone numbers, account and card numbers, anything where
007differs from7, go in quotes. - Don’t format numbers for people inside JSON. Locale formatting writes
1,5or1 000; thousands separators and decimal commas belong in the interface, not in data. - Convert at the boundary. Hex from a device,
+1from a form,1.from a spreadsheet: turn them into plain decimals where they enter your system. - Keep big numbers exact. A 19-digit ID is valid JSON, but
JSON.parserounds it; see big numbers in JSON.
Check numbers in the validator
Try it: open the invalid-number sample in the Validator. The card quotes the whole token, 0x1F and not just x, and points at its first character. Big integers and -0 pass: they are valid JSON, and the Formatter keeps them exactly as written.
- Invalid literal:
NaN,Infinityand-Infinity, which look like numbers and aren’t. - Missing comma: what several parsers report for
01and0x1F. - Unquoted key: what the decimal comma in
{"price": 1,5}turns into. - Big numbers in JSON
- All JSON errors
JSON numbers FAQ
Are leading zeros allowed in JSON numbers?
Only a single 0 before the decimal point, as in 0.5. 01, 007 and -01 are invalid; keep codes such as "02134" as strings.
Can a JSON number start with a dot or a plus sign?
No: 0.5, not .5; 1, not +1. A minus sign is allowed in front, a plus only in the exponent, as in 1e+5.
Does JSON support hexadecimal numbers?
No, only base 10. Write 31 for 0x1F, or keep the hex text as the string "0x1F".
Is 1,5 a valid JSON number?
No, JSON uses a dot. Inside an array [1,5] is valid but means two numbers, 1 and 5; inside an object {"a": 1,5} fails because 5 is read as the next key.