Parse json in javascript - long numbers get rounded

It's too big of a number. JavaScript uses double-precision floats for numbers, and they have about 15 digits of precision (in base 10). The highest integer that JavaScript can reliably save is something like 251.

The solution is to use reasonable numbers. There is no real way to handle such large numbers.


The largest number JavaScript can handle without loss of precision is 9007199254740992.


As others have stated, this is because the number is too big. However, you can work around this limitation by sending the number as a string like so:

var s = '{"x":"6855337641038665531"}';

Then instead of using JSON.parse(), you can use a library such as javascript-bignum to work with the number.


I faced this issue some time ago, I was able to solve using this lib: https://github.com/josdejong/lossless-json

You can check this example:

let text = '{"normal":2.3,"long":123456789012345678901,"big":2.3e+500}';
// JSON.parse will lose some digits and a whole number:
console.log(JSON.stringify(JSON.parse(text)));
// '{"normal":2.3,"long":123456789012345680000,"big":null}'      WHOOPS!!!
// LosslessJSON.parse will preserve big numbers:
console.log(LosslessJSON.stringify(LosslessJSON.parse(text)));
// '{"normal":2.3,"long":123456789012345678901,"big":2.3e+500}'