· 5 min read
Why JSON.parse changes large numbers, and how to keep them exact
JSON.parse silently rounds 64-bit IDs past 2^53. Learn why large numbers lose precision in JavaScript and how to fix it with strings or BigInt.

If an API returns a 64-bit ID such as 9007199254740993 and you read it with JSON.parse, JavaScript quietly gives you 9007199254740992. No error, no warning, just a different number. JSON numbers become JavaScript Number values, which are 64-bit floats that can only represent integers exactly up to 2^53 − 1. The fix is to send large IDs as strings, or to parse them as BigInt with the reviver's context.source argument.
The bug is easy to ship because small test data never triggers it. It shows up later, when real IDs grow past the safe range and two different records start to look identical.
Reproduce it
const text = '{"id": 9007199254740993, "n": 12345678901234567890}';
JSON.parse(text);
// { id: 9007199254740992, n: 12345678901234567000 }
9007199254740993 === 9007199254740992;
// true
Both numbers came back wrong. The first is off by one. The second lost its last few digits. Neither threw anything, so nothing in your logs will tell you it happened.
This hits real systems. Snowflake-style IDs (64-bit integers that pack a timestamp, a machine ID and a counter), BIGINT primary keys from Postgres or MySQL, payment amounts stored in minor units, and nanosecond timestamps all routinely go past 2^53. Other languages are often fine: Python's json.loads returns an exact arbitrary-precision int for the same input. So the backend team sees the right value and the frontend sees a different one.
Why does JSON.parse change large numbers?
The JSON format itself puts no limit on number size. The limit comes from the language reading it. JavaScript has one general number type, Number, which is an IEEE 754 double-precision float. A double has 53 bits of precision for the digits, so every integer from −(2^53 − 1) to 2^53 − 1 is stored exactly. That bound is exposed as Number.MAX_SAFE_INTEGER, which is 9007199254740991.
Past that point, the gap between representable values grows. Between 2^53 and 2^54 only even integers exist, so an odd number like 9007199254740993 gets rounded to the nearest even neighbour. Further out the gaps get wider still, which is why the 20-digit value lost several digits.
JSON.parse converts each number token straight into a Number. By the time your code sees the value, the original digits are gone.
| Value | Safe? | What JSON.parse returns |
|---|---|---|
9007199254740991 | yes | 9007199254740991 |
9007199254740992 | no | 9007199254740992 (looks right by luck) |
9007199254740993 | no | 9007199254740992 |
12345678901234567890 | no | 12345678901234567000 |
You can check a value with Number.isSafeInteger(v). It returns false for anything outside the safe range, including values that happen to round to themselves.
Fix 1: send large IDs as strings
The most robust fix is on the producing side. If a number is an identifier rather than something you do arithmetic on, serialize it as a string:
{ "id": "9007199254740993" }
Strings survive every JSON parser in every language unchanged. You can compare them, use them as map keys and put them in URLs without thinking about precision. Many large public APIs return IDs this way for exactly this reason, sometimes in both forms (a numeric id plus an id_str).
This is the one to remember for API design: IDs are labels, not quantities. Treat them as strings at the boundary.
Fix 2: parse big integers as BigInt with context.source
When you can't change the API, you can recover the exact digits on the client. The reviver function passed to JSON.parse now receives a third argument, context. For primitive values, context.source holds the original text of the token before it was converted. BigInt is JavaScript's arbitrary-precision integer type, so you can build one from that text:
function bigIntReviver(key, value, context) {
if (
typeof value === 'number' &&
/^-?\d+$/.test(context.source) &&
!Number.isSafeInteger(value)
) {
return BigInt(context.source);
}
return value;
}
JSON.parse('{"id": 9007199254740993, "price": 1.5, "count": 42}', bigIntReviver);
// { id: 9007199254740993n, price: 1.5, count: 42 }
The regular expression matters. Without it, a token like 1e20 or 1.5 would reach BigInt and throw a SyntaxError, because BigInt only accepts integer text. Small integers stay ordinary numbers, so the rest of your code keeps working. For objects and arrays, context is an empty object, which the typeof check already skips.
This is part of the JSON source text access feature. It is supported in Chrome and Edge 114, Firefox 135 and Safari 18.4, which made it Baseline in March 2025. It also works in current Node.js releases; the examples here were run on Node 22. For older browsers you still need a parsing library that handles big numbers.
Sending BigInt back out with JSON.rawJSON
Parsing is half the problem. JSON.stringify refuses BigInt outright:
JSON.stringify({ id: 9007199254740993n });
// TypeError: Do not know how to serialize a BigInt
The same feature adds JSON.rawJSON(text), which creates a value that JSON.stringify writes out verbatim. Combine it with a toJSON method, or a replacer, to emit the exact digits:
const replacer = (key, value) =>
typeof value === 'bigint' ? JSON.rawJSON(value.toString()) : value;
JSON.stringify({ id: 9007199254740993n }, replacer);
// '{"id":9007199254740993}'
JSON.rawJSON only accepts primitive JSON text such as a number, string, boolean or null. Passing an object or array string throws. Prefer a replacer over patching BigInt.prototype.toJSON globally, since a global patch changes behavior for every library in the page. For other ways JSON.stringify silently changes your data, see why JSON.stringify drops undefined, Map, Set and Date.
Where else does this bite?
response.json()infetchuses the same parsing rules and takes no reviver. To use a reviver, readawait response.text()and callJSON.parseyourself.- Database drivers often return
BIGINTcolumns as strings orBigInton purpose. Converting them withNumber(...)reintroduces the bug. Number("9007199254740993")rounds exactly likeJSON.parsedoes.BigIntandNumberdon't mix in arithmetic.1n + 1throws aTypeError, so decide at the boundary which type each field uses.
Key takeaways
JSON.parseturns every number into a double, so integers above 2^53 − 1 silently lose precision.- Check suspicious values with
Number.isSafeInteger. - The best fix is to send IDs and other large integers as strings.
- If you can't change the API, use a reviver with
context.sourceandBigIntto keep the exact digits. - Use
JSON.rawJSONin a replacer to serializeBigIntvalues back as plain JSON numbers.
FAQ
Is this a bug in JSON or in JavaScript?
Neither, strictly. JSON allows numbers of any size; JavaScript's Number type can't hold all of them exactly. Parsers in other languages may keep the full value, which is why the mismatch often appears only on the frontend.
Can I just use parseFloat or Number on the string?
No. Both produce a Number, which has the same 53-bit limit. Use BigInt(text) for exact integers, or keep the value as a string.
Does TypeScript catch this?
No. The value is still typed as number, and the rounding happens at runtime. A type of string or bigint for ID fields makes the intent visible, but the parsing still has to be done correctly.