Why Does JSON.parse Change Big Numbers?

Paste this into your browser console. Then look closely at the result:

JSON.parse('{"id": 12345678901234567890}').id
// → 12345678901234567000

The last three digits changed, and nothing warned you. What if that number is an order ID, a database key or a social media post ID? Now your code points at the wrong record.

This article explains why this happens. It also shows other ways numbers can change without any warning. And it shows you how to keep them exact.

Quick answer: In JavaScript, every number uses the same format. That format can only hold whole numbers exactly up to 2^53 − 1. JSON.parse rounds bigger whole numbers while it reads them. To keep big IDs exact, send them as strings, or use a parser that keeps the original text.

Why it happens: JavaScript has one number type

JSON itself has no limit on how big or how exact a number can be. JavaScript does have a limit.

In JavaScript, every number is a 64-bit floating-point value. The official name for this format is IEEE 754 double precision. It keeps about 15–17 significant digits (the digits that carry meaning). It can only store whole numbers exactly up to 2^53 − 1:

Number.MAX_SAFE_INTEGER // 9007199254740991

Any integer (a whole number) bigger than that gets rounded. It becomes the nearest value the format can hold.

JSON.parse reads JSON text and turns it into JavaScript values. It has to give you a number, so the rounding happens while it reads. Your code never sees the first value.

Then you may turn the data back into text with JSON.stringify. It writes out the rounded number. The original number is lost forever.

It is not only big whole numbers

  • Trailing zeros disappear. 1.10 becomes 1.1. This matters for prices, version numbers and anything you compare as text.
  • Exponents are rewritten. 1E3 comes back as 1000.
  • Numbers that are too big turn into null. 1e400 becomes Infinity when it is read. Then JSON.stringify(Infinity) writes null.
  • Long decimals get shorter. 0.1000000000000000055511151231257827 becomes 0.1.

Many online JSON formatters and viewers use JSON.parse inside. So just “beautifying” your data (making it neat) in one of them can change it.

Here is a good test. Format {"id": 12345678901234567890, "price": 1.10} and check that both values stay the same.

The best fix: send big IDs as strings

If you control the API, send IDs as strings: {"id": "12345678901234567890"}.

IDs are labels, not amounts. You never add them together. So a string is the right type for them anyway.

Twitter’s API is a well-known example. It added an id_str field next to every number id for this exact reason.

The same goes for money. Send amounts as strings ("19.90"). Or send them as whole numbers in the smallest unit (1990 cents).

When you can’t change the API

Use a parser that keeps the original text

Libraries such as lossless-json and json-bigint don’t turn numbers into plain numbers. They read them into special objects or BigInt values instead, so nothing is lost. BigInt is a JavaScript type for very big whole numbers.

These libraries are slower than the built-in parser. So use them only where it matters. This is usually the place where data first comes into your app.

Read the source text in a reviver

A reviver is a function you can pass to JSON.parse. It gets called for every value. Newer JavaScript engines pass it a third argument called context. This argument holds the original text of each value.

This feature comes from the “JSON.parse source text access” proposal. Where it works, you can turn unsafe whole numbers into BigInt:

const data = JSON.parse(text, (key, value, context) =>
  typeof value === 'number' &&
  !Number.isSafeInteger(value) &&
  /^-?\d+$/.test(context?.source ?? '')
    ? BigInt(context.source)
    : value,
);

Before you rely on it, check that every browser and runtime you use supports it. Older browsers don’t pass context. In that case, the code above leaves the value as it is.

Other languages have the same problem

  • Python keeps whole numbers exact, but decimals become floats (decimal numbers that are not always exact). For money, use json.loads(text, parse_float=decimal.Decimal).
  • Go reads numbers as float64 when the target is interface{}. Call decoder.UseNumber() to keep them as json.Number strings.
  • Java (Jackson) can use BigDecimal for decimal values. Turn on DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS to do this.
  • Databases and spreadsheets have their own limits. Excel, for example, keeps only 15 significant digits.

Checklist

  • In APIs you design, send IDs and money as strings (or as whole cents).
  • For JSON from other systems, use a lossless parser where exact numbers matter. A lossless parser loses nothing.
  • Test your whole data flow with values like 12345678901234567890 and 1.10.
  • Use tools that don’t round numbers. Every tool on JSONPilot keeps numbers exactly as written. This includes the formatter, validator, viewer and converters.

Common questions

What is the biggest safe whole number in JavaScript?

It is Number.MAX_SAFE_INTEGER, which is 9007199254740991 (2^53 − 1). Whole numbers above this may be rounded.

Is the number wrong in the JSON file itself?

No. JSON has no size limit for numbers, so the file is fine. The change happens when JSON.parse reads it.

Should I send IDs as numbers or strings?

Send big IDs as strings. You never do math with IDs, and a string keeps every digit exact.

Why does 1.10 become 1.1?

JavaScript stores the number, not the text you wrote. 1.10 and 1.1 are the same number, so the extra zero is lost. If the zero matters, send the value as a string.