JSON Repair Tool

Why Does JSON Round Large Numbers? The 2^53-1 Precision Trap, Fixed

Why does JSON round large numbers? The unsettling part is that the JSON is fine. The digits in the text are exact. The rounding happens later, when JavaScript converts those digits into a Number, and the fix is to get the integer into a string before that conversion ever happens.

Watch it happen once and you will never trust a big numeric ID again. An API returns {"transactionId": 9007199254740993}. Open the browser's network tab and the response looks perfect, every digit in place. Now run JSON.parse on that same text and log the result. You get 9007199254740992. One digit changed, no error thrown, no warning logged. Your ID no longer matches the database row, and nothing told you.

That silent corruption is the whole problem. Two distinct IDs can compare equal. Sorts that worked in testing go unstable in production. A lookup by ID misses, and the failure looks like a data problem, not a parsing problem.

Why does JSON round large numbers: the parser or the number?

It is the number. JavaScript has exactly one numeric type: an IEEE 754 double-precision float. It stores integers exactly only up to 2^53 - 1, which is 9,007,199,254,740,991 (Number.MAX_SAFE_INTEGER). Past that line, adjacent integers map to the same stored value, so the engine quietly rounds to the nearest representable float.

That means the damage can happen twice. It happens when your code evaluates a literal that big, because the literal is rounded before anything else runs: 9007199254740993 === 9007199254740992 is true in JavaScript, which is a genuinely disturbing sentence to read. And it happens again when JSON.parse converts the text into a Number. RFC 8259, the JSON spec, defines only the grammar of numbers as decimal text. It never prescribes one in-memory numeric type, so a producer can emit perfectly valid JSON that JavaScript cannot store exactly.

The cleanest proof that the format is innocent: run the same payload through three runtimes.

node    -p 'JSON.parse("{\\"id\\":9007199254740993}").id'
# 9007199254740992
python3 -c 'import json; print(json.loads("{\\"id\\":9007199254740993}")["id"])'
# 9007199254740993
ruby    -rjson -e 'p JSON.parse("{\\"id\\":9007199254740993}")["id"]'
# 9007199254740993

Python and Ruby keep the value exact because their integers are arbitrary precision. JavaScript is the odd one out, and it is the language most JSON is written in.

Where this bites in real systems

The values that cross the line are not exotic. Nineteen-digit Snowflake-style IDs from distributed databases sit well past it. Nanosecond timestamps do. Large monetary amounts expressed in minor units do. A price of a few million dollars stored as integer cents is around 10^9, safely under the limit, but an invoice total in a smaller currency or a cumulative ledger balance can drift over it. Account numbers and transaction references from banking systems regularly land above it too.

The failure mode is always the same shape: a producer sends the correct digits while the receiving JavaScript program silently stores a different number. Validation performed after parsing cannot recover the original value, because the distinction has already disappeared by the time your validation code runs.

The fix: keep big integers as strings on the wire

The reliable fix happens at the producer. Send the value quoted:

{"id": "9007199254740993"}

The digits travel untouched because a string is never converted to a float. On the JavaScript side, convert to BigInt or use a decimal library when you need to do math. If you cannot change the producer, parse the payload with a lossless JSON parser (json-bigint and lossless-json are the common choices) for endpoints you know carry big integers, or extract the field from the raw text before running JSON.parse.

Two smaller habits are worth adopting. Compare big numeric IDs as strings, not as parsed numbers. And know that JSON.stringify throwing TypeError: Do not know how to serialize a BigInt is not a bug. JSON has no BigInt type, and the engine refuses the conversion rather than choosing an encoding that would silently lose data downstream, which is exactly the corruption this article is about.

The naive auto-fix that makes it worse

Once people learn about the precision trap, the instinct is to have the repair tool quote every large number. That creates new problems. A quoted "1.5e21" is no longer a number at all, so code that multiplies it gets string concatenation instead of math. Scientific notation values, which JSON allows, become unparseable as numbers on the receiving end. And quoting a value that was already fine, like a price of 19.99, changes its type for every downstream consumer.

The right repair rule is narrower: quote only integers with more than 15 digits that are not already strings, and flag them for a human to confirm. Fifteen digits is the practical boundary because 2^53 is roughly 9 times 10^15, so any integer with 16 or more digits is suspect. Our repair tool applies exactly this rule and lists every value it changed, so you can check each one instead of trusting a silent rewrite.

The one-line test. Paste your payload and check any integer above 9,007,199,254,740,991. If a value sits past that line and arrives unquoted, it is already rounded by the time your code sees it. Quote it at the source or the exact digits are gone.

Broken payload on your screen?

Paste it in and get valid JSON back, with each unsafe value flagged and quoted automatically.

Repair My JSON Free

Frequently asked questions

Is JSON itself lossy with large numbers?

No. RFC 8259 defines JSON numbers as decimal text, and the text holds the exact digits. Precision is lost when a parser converts those digits into a fixed-width numeric type, like JavaScript's IEEE 754 double. The same document parsed in Python or Ruby keeps the value exact.

What is the largest exact integer in JavaScript?

9,007,199,254,740,991, which is 2^53 minus 1 (Number.MAX_SAFE_INTEGER). Integers above that line are rounded to the nearest representable float. The rounding happens for literals in your source code too, before JSON.parse is ever called.

How do I keep a 19-digit ID exact through JSON?

Quote it as a string at the producer ({"id": "9007199254740993"}), convert it to BigInt on the JavaScript side, and compare IDs as strings. If you cannot change the producer, parse with a lossless JSON parser such as json-bigint or lossless-json for those payloads.

Why does JSON.stringify throw on BigInt?

JSON has no BigInt value type, so the engine throws a TypeError instead of silently picking an encoding that downstream parsers (Python, Go, a 64-bit database column) might read differently. The refusal is deliberate data-loss prevention.

Can I detect precision loss after parsing?

Not reliably, because the rounded value replaces the original. Number.isSafeInteger tells you whether a Number is exact, but by the time you call it the damage is done. Check the raw text before parsing, or fix the producer to quote the value.