JSON Numbers & Precision
JSON numbers look simple — 42, 3.14, -1e9 — but they hide a trap. The JSON standard puts no limit on the size or precision of a number, while most parsers store numbers as 64-bit floating-point values (IEEE 754 "doubles"). When the text holds more precision than a double can, data is silently changed.
Number syntax recap
| Part | Example | Rule |
|---|---|---|
| Sign | -5 |
Only minus; no + |
| Integer part | 0, 120 |
No leading zeros (except 0 itself) |
| Fraction | 0.25 |
Needs digits on both sides of . |
| Exponent | 6.02e23, 1E-9 |
e/E, optional sign |
The safe integer limit
JavaScript numbers can represent every integer exactly only up to 2^53 − 1 = 9,007,199,254,740,991 (Number.MAX_SAFE_INTEGER). Larger integers are rounded to the nearest representable value:
console.log(Number.MAX_SAFE_INTEGER);
const data = JSON.parse('{"id": 9007199254740993, "small": 42}');
console.log(data.id); // last digit changed!
console.log(Number.isSafeInteger(data.id), Number.isSafeInteger(data.small));
console.log(JSON.parse("9007199254740993") === JSON.parse("9007199254740992"));9007199254740991
9007199254740992
false true
true
No error, no warning — the ID is simply wrong. This is a real problem with 64-bit IDs from databases and services such as social networks, which is why many APIs send big IDs as strings as well:
{
"id": 1846382039471820811,
"id_str": "1846382039471820811"
}Valid JSON: object with 2 keys
{
"id": 1846382039471820800,
"id_str": "1846382039471820811"
}
The validator here runs on JSON.parse, so even the pretty-printed copy shows the rounded number. The id_str field survives exactly.
Decimal fractions
Doubles store fractions in binary, so many decimal values can't be represented exactly. This is a general floating-point issue, not a JSON one, but it shows up whenever you calculate with parsed values:
const cart = JSON.parse('{"items": [0.1, 0.2], "price": 1.15, "qty": 3}');
console.log(cart.items[0] + cart.items[1]);
console.log(cart.price * cart.qty);
// Common fix for money: work in whole cents
const cents = Math.round(cart.price * 100) * cart.qty;
console.log(cents, "cents =", (cents / 100).toFixed(2));0.30000000000000004
3.4499999999999997
345 cents = 3.45
Tip: For money, send integer minor units (
"amountCents": 1999) or a decimal string ("amount": "19.99") together with a currency code.
Numbers that JSON cannot express
NaN, Infinity and -Infinity have no JSON spelling. JSON.stringify quietly writes null instead:
const stats = { avg: 0 / 0, max: 1 / 0, min: -1 / 0, negZero: -0 };
console.log(JSON.stringify(stats));{"avg":null,"max":null,"min":null,"negZero":0}
Note that -0 also loses its sign. If these special values matter, encode them explicitly, e.g. as the strings "NaN" or "Infinity".
Getting exact big integers in JavaScript
The BigInt type can hold arbitrarily large integers, but JSON.parse will never produce one by itself, and JSON.stringify throws on it. The usual approach is: send big numbers as strings and convert them deliberately.
const text = '{"account": "123456789012345678901234567890", "balance": 10}';
const obj = JSON.parse(text, (key, value) =>
key === "account" ? BigInt(value) : value
);
console.log(typeof obj.account, obj.account + 1n);
const out = JSON.stringify(obj, (key, value) =>
typeof value === "bigint" ? value.toString() : value
);
console.log(out);bigint 123456789012345678901234567891n
{"account":"123456789012345678901234567890","balance":10}
Note: Recent JavaScript engines also pass a third
contextargument to revivers, containing the original source text of each primitive — useful for exact big numbers. Check browser support before relying on it.
Other languages
| Language | Default behaviour |
|---|---|
| JavaScript | Everything becomes a double |
| Python | Integers stay exact (int has no size limit); fractions become float |
| Java (Jackson) | int/long, or BigInteger/BigDecimal when configured |
| Go | float64 unless you use json.Number or a typed struct |
import json
from decimal import Decimal
data = json.loads('{"id": 9007199254740993, "price": 1.15}')
print(data["id"]) # exact in Python
print(data["price"] * 3) # float rounding
exact = json.loads('{"price": 1.15}', parse_float=Decimal)
print(exact["price"] * 3) # exact decimal arithmetic9007199254740993
3.4499999999999997
3.45
Test yourself
What is Number.MAX_SAFE_INTEGER in JavaScript?
- 2^31 − 1
- 2^53 − 1
- 2^64 − 1
- There is no limit
How should an API send a 64-bit database ID to JavaScript clients safely?
- As a number with an exponent
- As a string
- Split into two booleans
- As NaN
What does JSON.stringify({x: NaN}) produce?
- {"x":NaN}
- {"x":"NaN"}
- {"x":null}
- A TypeError