Why big integers change in JSON.parse
JavaScript has one ordinary number type: an IEEE 754 double-precision float. A double has 53 bits of precision, so it can represent every integer from -9007199254740991 to 9007199254740991 (that is ±(253 − 1), exposed as Number.MAX_SAFE_INTEGER). Above that the gaps between representable values grow: first every second integer, then every fourth, and so on. JSON.parse converts each number in the text to the nearest double, and it does so without an error or a warning.
JSON.parse('{"id": 9007199254740993}').id // 9007199254740992
JSON.parse('{"id": 1453729827150749697}').id // 1453729827150749700
Number.isSafeInteger(9007199254740993) // false
JSON.stringify(JSON.parse('{"id": 12345678901234567890}'))
// '{"id":12345678901234567000}' the round trip rewrote the ID
The JSON format itself has no size limit. RFC 8259 only warns that numbers beyond double precision may not be read the same way everywhere. So the JSON is valid, the parser keeps working, and your data is wrong. That is why this bug usually turns up much later, for example as "record not found" for an ID that exists, or as two different users who suddenly share one ID.
Where you will meet it
| Source | Typical size | Safe in JS? |
|---|---|---|
| Twitter / X snowflake IDs | ~1.4 × 1018 (19 digits) | No. The API sends id_str alongside id, so use id_str. |
| Discord snowflakes | 17–19 digits | No. Discord's API sends them as strings. |
Database BIGINT / int64 keys | up to 9.2 × 1018 | Only while they stay below 253 |
| Snowflake-style IDs from your own services (Sonyflake, Instagram-style IDs) | usually 64-bit | No |
| Nanosecond timestamps | ~1.7 × 1018 | No (millisecond timestamps are fine) |
| Money as integer cents, counters | usually small | Yes, below 9 quadrillion |
Fix 1 (best): send big numbers as strings
If you control the API, serialise 64-bit IDs as strings. {"id": "1453729827150749697"} survives every parser in every language, and IDs are not used for arithmetic anyway. Most frameworks have a switch for this: in .NET JsonNumberHandling.WriteAsString, in Jackson @JsonSerialize(using = ToStringSerializer.class) on the field, in Go the ,string struct tag option (`json:"id,string"`). For existing documents, the Convert big numbers to strings button above does exactly this.
Fix 2: read the original digits with a JSON.parse reviver
Newer JavaScript engines implement the TC39 proposal JSON.parse source text access. The reviver gets a third argument, context, and for primitive values context.source is the exact text from the JSON:
const data = JSON.parse(text, (key, value, context) =>
typeof value === "number" && !Number.isSafeInteger(value) && context && /^-?\d+$/.test(context.source)
? BigInt(context.source) // or keep context.source as a string
: value
);
// The same proposal adds JSON.rawJSON for the other direction:
JSON.stringify({ id: JSON.rawJSON("12345678901234567890") }); // '{"id":12345678901234567890}'
Support is not universal yet. It has shipped in V8, so in Chrome, Edge and current Node.js (the snippet above was tested on Node.js 24). Check MDN for the current Firefox and Safari status. Older engines do not have it. There the third argument is simply undefined, which is why the snippet checks context. Feature-detect before relying on it:
const hasSource = JSON.parse("1", (k, v, ctx) => ctx && ctx.source === "1");
Fix 3: a big-number JSON library
// npm install json-bigint
const JSONbig = require("json-bigint");
JSONbig.parse('{"id": 12345678901234567890}').id.toString(); // "12345678901234567890" (BigNumber)
const JSONnative = require("json-bigint")({ useNativeBigInt: true });
JSONnative.parse('{"id": 12345678901234567890}').id; // 12345678901234567890n
const JSONstr = require("json-bigint")({ storeAsString: true });
JSONstr.parse('{"id": 12345678901234567890}').id; // "12345678901234567890"
Using BigInt has a cost: JSON.stringify throws TypeError: Do not know how to serialize a BigInt unless you give it a replacer, and you cannot mix BigInt and number in arithmetic. For IDs, strings are usually simpler.
Other languages
| Language | Default behaviour | How to keep exact values |
|---|---|---|
Python json | Integers are exact (Python ints have no size limit). Decimals become float. | json.loads(text, parse_float=decimal.Decimal) for decimals |
Go encoding/json | Into a typed int64 / uint64 field: exact. Into interface{} / any: float64, so rounded. | dec := json.NewDecoder(r); dec.UseNumber() gives json.Number (the original text). Or decode into typed structs. |
| Java (Jackson) | Into long / Long: exact up to 263 − 1. Untyped (Object, JsonNode): int, long or BigInteger depending on size, so exact. Decimals become double. | DeserializationFeature.USE_BIG_INTEGER_FOR_INTS, USE_BIG_DECIMAL_FOR_FLOATS |
| C# System.Text.Json | Exact when you read into long or decimal | Typed models; JsonElement.GetRawText() for the original text |
jq | Versions before 1.7 convert to double. jq 1.7 keeps the original literal when the number passes through unchanged. | Upgrade, or treat IDs as strings |
The JavaScript client is usually where precision is lost, even when every backend handles the number correctly. So the safest fix is the one that works for the weakest reader: strings.
Decimals lose digits too
The same 53-bit limit applies to fractions. 3.14159265358979323846 becomes 3.141592653589793, and 0.1 + 0.2 is 0.30000000000000004. For prices, keep them as integer minor units (1999 cents) or as decimal strings ("19.99"). The checker above also flags long decimals that would lose digits.
How NeatJSON avoids the problem
Most online JSON tools call JSON.parse and then JSON.stringify, so formatting a document can silently rewrite its IDs. NeatJSON's parser keeps every number as the exact text it was written with. Formatting, minifying, sorting keys, repairing and converting never change a digit. When a number would be rounded by JSON.parse, the tools tell you.
FAQ
What is the largest integer JSON.parse can handle exactly?
9007199254740991, which is 2^53 − 1 and available as Number.MAX_SAFE_INTEGER. Beyond that, a JavaScript number cannot hold every integer, so JSON.parse rounds to the nearest value it can store. 9007199254740993 becomes 9007199254740992.
Does the JSON format limit the size of numbers?
No. The JSON grammar allows any number of digits. RFC 8259 warns that interoperability is only good within the precision of IEEE 754 doubles, that is between −(2^53 − 1) and 2^53 − 1, because many parsers store numbers as doubles. The rounding is done by the parser, not the format.
Why does JSON.parse change my ID without an error?
Converting a long decimal string to a double always succeeds. It just picks the nearest representable value. JavaScript has no warning for this, so the wrong ID flows through your code silently.
How do I parse JSON with big integers in JavaScript?
Best: have the server send them as strings. Otherwise use a JSON.parse reviver with context.source where it is supported, for example JSON.parse(text, (k, v, ctx) => typeof v === "number" && !Number.isSafeInteger(v) && ctx ? BigInt(ctx.source) : v), or a library such as json-bigint.
Why do Twitter and Discord send IDs as strings?
Their IDs are 64-bit snowflakes, far above 2^53. Twitter's API v1.1 returns both id and id_str and tells JavaScript developers to use id_str. Discord's API serialises snowflakes as strings for the same reason.
Does Python lose precision on big JSON numbers?
Not for integers: json.loads returns an exact Python int of any size. Numbers with a decimal point or exponent become float by default. Pass parse_float=decimal.Decimal to keep them exact.
Does NeatJSON round big numbers?
No. The formatter, validator and converters on NeatJSON keep each number exactly as written in the text, and they warn you when a number would be rounded by JSON.parse.