Guide

Handling big numbers in JSON without losing precision

Published November 26, 2026 · 4 min read

Large integers such as 64-bit database IDs can silently change when JavaScript parses JSON. This guide explains why, and shows three reliable fixes.

The problem in one line

JSON.parse('{"id": 9007199254740993}').id
// 9007199254740992   ← not the number you sent

No error, no warning: the ID is simply off by one. If that ID is used to look up an order, a user or a payment, the code silently fetches the wrong record or nothing at all.

Why it happens

JSON itself has no limit on number size. The problem is the language that reads it. JavaScript stores every number as a 64-bit floating-point value (IEEE 754 double). That format has 53 bits for the digits, so it can represent every integer exactly only up to 253 − 1:

Number.MAX_SAFE_INTEGER       // 9007199254740991
Number.isSafeInteger(9007199254740993) // false

Above that, only some integers can be represented, and anything else is rounded to the nearest one that can. The bigger the number, the bigger the gaps.

Values that commonly go past the limit:

  • 64-bit database IDs, such as PostgreSQL bigint or MySQL BIGINT columns, which can hold values up to about 9.2 × 1018.
  • Snowflake-style IDs that encode a timestamp, used by many large platforms. This is why some public APIs, for example X (Twitter) with id_str and Discord, send IDs as strings.
  • Nanosecond timestamps such as 1767225600000000000.
  • Large financial or scientific values stored as integers.

Decimals have a related problem

Floating point also can’t represent most decimal fractions exactly, which is why 0.1 + 0.2 is 0.30000000000000004. Parsing 0.12345678901234567890 gives 0.12345678901234568. For money, don’t send amounts as JSON decimals that you then calculate with. Send integer minor units ("amount_cents": 1999) or a decimal string ("amount": "19.99") and use a decimal library for arithmetic.

How to tell if you are affected

  • IDs that end in zeros or come out one or two off from the database value.
  • Lookups by ID failing only for newer records (whose IDs have grown larger).
  • The raw response in your browser’s Network tab shows a different number from what your code logs.

Paste the raw response into the 2JSON viewer. It parses numbers losslessly, so it shows the exact value the server sent, which you can compare with what your code sees.

Fix 1: send big numbers as strings (recommended)

The simplest and most portable fix is on the API side: send identifiers as strings.

{ "id": "9007199254740993", "name": "Ada" }

Every language reads a string exactly, and IDs are labels you compare and pass around, not numbers you add or multiply. Many APIs do this for all IDs, even small ones, so clients never have to think about it. If you must keep the numeric field for older clients, add a string version next to it, as in "id": 123, "id_str": "123".

When serializing in JavaScript, a replacer converts BigInt values to strings:

JSON.stringify({ id: 9007199254740993n }, (key, value) =>
  typeof value === 'bigint' ? value.toString() : value
)
// '{"id":"9007199254740993"}'

Fix 2: read the original text with the reviver context

If you can’t change the API, newer JavaScript engines pass a third argument to the JSON.parse reviver containing the original source text of each primitive value. You can turn unsafe integers into BigInt before precision is lost:

const data = JSON.parse(text, (key, value, context) => {
  const src = context?.source
  // Only whole numbers written without a decimal point or exponent can become BigInt.
  if (typeof value === 'number' && !Number.isSafeInteger(value) && /^-?\d+$/.test(src)) {
    return BigInt(src)
  }
  return value
})

The matching JSON.rawJSON() lets you write an exact number back out: JSON.stringify({ id: JSON.rawJSON('12345678901234567890') }) produces {"id":12345678901234567890}.

These are recent additions to JavaScript. They work in current versions of Chrome, Edge, Firefox and Node.js, but check support for every browser you target (for example on caniuse.com) before relying on them. The context?.source check above keeps the code working, with the old behaviour, where they aren’t supported.

Fix 3: use a lossless JSON library

Libraries such as lossless-json and json-bigint replace JSON.parse with a parser that keeps numbers exact, returning them as special number objects, strings or BigInt. They work in every environment, at the cost of a dependency and slower parsing. 2JSON uses lossless-json, which is why the viewer shows big numbers exactly.

Other languages

  • Python: integers have unlimited size, so json.loads keeps them exact. Decimals become float; pass parse_float=decimal.Decimal to keep them exact too.
  • Go: decoding into a struct field of type int64 or uint64 is exact. Decoding into any (interface{}) gives float64 and loses precision. Call decoder.UseNumber() to get json.Number strings instead.
  • Java and Kotlin: map fields to long for 64-bit values, or BigInteger and BigDecimal for anything larger or decimal.
  • Databases: PostgreSQL’s jsonb stores numbers as exact numeric values, but the client library that reads them may still convert to a float.

The risk is highest when a backend in one of these languages sends exact 64-bit integers to a JavaScript frontend, which is a very common setup.

Checklist

  • Send IDs and other large integers as strings in your APIs.
  • Send money as integer minor units or decimal strings, never as floats to calculate with.
  • When you can’t change the API, parse with the reviver context.source or a lossless library.
  • Use Number.isSafeInteger() in tests to catch values that are already too large.
  • Check suspicious values against the raw response in a lossless viewer like 2JSON.