Development

Parse Large JSON Numbers Without Losing Precision

Why JSON.parse rounds 64-bit IDs, and four tested fixes: string IDs, the reviver's source text, json-bigint, and writing BigInt back with JSON.rawJSON.

The Debuggers · Updated · 8 min read

If your API sends a 64-bit integer, such as a database ID or a Snowflake-style ID from Discord or X, JavaScript can read it as a different number without any error:

JSON.parse('{"id": 9223372036854775807}').id;
// 9223372036854776000

The last few digits have changed. If you then use that ID to fetch or update a record, you get the wrong record or none at all. This guide explains why it happens and shows four fixes. All the code below was run in Node.js 22.

Why the number changes

JSON.parse turns every JSON number into a JavaScript Number, which is a 64-bit floating point value. That format has 53 bits for the digits of the number, so integers are only exact up to Number.MAX_SAFE_INTEGER, which is 9,007,199,254,740,991. Above that, not every integer can be represented, and the parser rounds to the nearest one that can:

9007199254740993 === 9007199254740992; // true

The JSON itself is fine. The data is lost at the moment of parsing, which is why you cannot fix it afterwards.

Fix 1: send large IDs as strings

If you control the API, this is the simplest and most portable fix. IDs are labels, not quantities: you never add them together, so there is no reason for them to be numbers in JSON.

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

Twitter's v1.1 API returned both id and id_str on every object for exactly this reason, and the current X API returns IDs only as strings. Do the same for any value that can exceed nine quadrillion, including counters in high-volume systems.

Fix 2: read the source text in a reviver

When you cannot change the API, current JavaScript engines let a reviver see the original text of each value. The reviver gets a third argument, context, and context.source holds the number exactly as it was written in the JSON. This works in Chrome and Edge 114, Firefox 135, Safari 18.4 and Node.js 21 or later.

function parseWithBigInt(text) {
  return JSON.parse(text, (key, value, context) =>
    typeof value === "number" &&
    !Number.isSafeInteger(value) &&
    /^-?\d+$/.test(context?.source ?? "")
      ? BigInt(context.source)
      : value
  );
}

parseWithBigInt('{"id": 9223372036854775807, "count": 42, "price": 19.99}');
// { id: 9223372036854775807n, count: 42, price: 19.99 }

Only integers that are too big become BigInt. Small numbers and decimals stay ordinary numbers, so the rest of your code is unaffected. The regular expression skips values written in exponent form, such as 1e20, which BigInt() cannot read.

Fix 3: use json-bigint in older environments

For older browsers or Node.js 20 and earlier, a parser written in JavaScript can read the digits itself. json-bigint is the most widely used. By default it returns BigNumber objects; the useNativeBigInt option returns native BigInt instead:

const JSONbig = require("json-bigint")({ useNativeBigInt: true });

const data = JSONbig.parse('{"id": 9223372036854775807, "count": 1}');
data.id; // 9223372036854775807n
JSONbig.stringify(data); // '{"id":9223372036854775807,"count":1}'

It also has a storeAsString option that returns large numbers as strings. The package has not had a release since 2022 but is stable. A pure JavaScript parser is slower than the built-in one, so for large responses prefer Fix 2 where it is available.

Fix 4: write BigInt back to JSON

The other half of the problem is sending the value back. JSON.stringify throws on a BigInt:

JSON.stringify({ id: 1n }); // TypeError: Do not know how to serialize a BigInt

JSON.rawJSON(), available in the same environments as Fix 2, lets a replacer write the exact digits as a JSON number:

function stringifyWithBigInt(value, space) {
  return JSON.stringify(
    value,
    (key, v) => (typeof v === "bigint" ? JSON.rawJSON(v.toString()) : v),
    space
  );
}

stringifyWithBigInt({ id: 9223372036854775807n }); // '{"id":9223372036854775807}'

If the API accepts string IDs, returning v.toString() instead is simpler and works everywhere.

Other languages

Python's int has unlimited size, so json.loads reads large integers exactly. Java (Jackson) and C# (System.Text.Json) read them exactly into long. Go is the exception: decoding into map[string]interface{} gives float64, so call decoder.UseNumber() to get json.Number values instead. Even when your back end is safe, a JavaScript front end, or a jq version older than 1.7, may round the same value later.

Checking a response

Online JSON tools, including our JSON Formatter, parse the text with the browser's JSON.parse, so they show the rounded value too. To see what the server actually sent, look at the raw response in your browser's network panel, or check the digits with a tool that does not parse numbers, such as curl. Our JSON formatting best practices guide covers other ways to keep JSON data safe as it moves between systems.

Frequently Asked Questions

What is the largest integer JSON.parse can read exactly?

9,007,199,254,740,991, which is 2 to the power 53 minus 1 and is available as Number.MAX_SAFE_INTEGER. Larger integers are rounded to the nearest number a 64-bit float can hold, so they can come back as a different number.

Can a JSON.parse reviver fix large numbers?

Yes, in current browsers and Node.js 21 or later. The reviver receives a third argument, context, whose source property is the original text of the number, so you can turn it into a BigInt before precision is lost. In older environments the reviver only sees the rounded number.

Why does JSON.stringify throw on a BigInt?

JSON has no BigInt type, so JSON.stringify refuses to guess and throws a TypeError. Convert BigInt values to strings in a replacer, or use JSON.rawJSON to write them out as plain JSON numbers.

Is this only a JavaScript problem?

Mostly. Python, Java and C# read 64-bit integers exactly by default. Go's encoding/json reads numbers into float64 when you decode into interface{}, so it has the same problem unless you call UseNumber on the decoder.

Want help with this in a real project?

Our team builds web and mobile software, from planning and design to launch and support.