A JSON number is written in decimal. The grammar puts no limit on its size or precision, but software does, so a number that is valid JSON may not reach a program intact.
Grammar
number = [ minus ] int [ frac ] [ exp ]
int = zero / ( digit1-9 *DIGIT )
frac = decimal-point 1*DIGIT
exp = e [ minus / plus ] 1*DIGIT
decimal-point = %x2E ; .
digit1-9 = %x31-39 ; 1-9
e = %x65 / %x45 ; e E
minus = %x2D ; -
plus = %x2B ; +
zero = %x30 ; 0
| Written | Valid? | Why |
|---|---|---|
0, -0, 12, -3.5 | Yes | |
1e3, 1E3, 1e+3, 2.5e-4 | Yes | The exponent’s e may be either case, and its sign is optional |
+1 | No | Only a minus sign may lead a number |
01 | No | A number has no leading zeros |
.5 | No | A digit is required before the decimal point |
5. | No | A digit is required after the decimal point |
1e | No | A digit is required after the exponent’s e |
0x1F | No | Hexadecimal is not allowed |
Infinity and NaN cannot be written: RFC 8259 says numeric values that cannot be represented in this grammar are not permitted.
What software can hold
RFC 8259 section 6 lets an implementation set limits on the range and precision of numbers. Since the IEEE 754 binary64 (“double precision”) type is so widely available, it says good interoperability comes from expecting no more range or precision than that type provides.
| Number | In a 64-bit float |
|---|---|
9007199254740991, which is 253 − 1 | Exact: integers from −(253 − 1) to 253 − 1 are the range RFC 8259 says every implementation agrees on |
12345678901234567890 | Rounded to the nearest float, so the low digits are lost |
1E400 | Too large: RFC 8259’s example of a number that may cause trouble |
1E-400 | Too small: it reads as zero |
3.141592653589793238462643383279 | RFC 8259’s other example: more digits than the type holds, so it is rounded |
Identifiers and other large integers are therefore often written as strings,
"12345678901234567890", so that no reader rounds them.
A number has no type beyond this: JSON does not distinguish integers from floats, and
1, 1.0 and 1e0 are simply different ways of
writing a number. How a program holds each is up to the program.