JSON allows integers of any size, but most parsers store them as IEEE 754 doubles, so 64-bit IDs above 2^53 come back silently rounded. Here is why it happens, which runtimes do it, and the wire contract that prevents it.
JSON itself has no limit on how big an integer can be, but most of the code that reads it does: any parser that stores numbers as IEEE 754 doubles silently rounds integers above 2^53, so a 64-bit ID can come out the other side as a different, perfectly valid-looking ID. The fix is not a cleverer parser on one side; it is a wire contract that says large integers travel as strings.
Here is the bug in one line of JavaScript:
JSON.parse('{"id": 1861234567890123456}').id
// 1861234567890123500
No exception, no warning. The parser did exactly what the language spec says it should, and the last digits of your ID are gone.
RFC 8259 defines a number grammatically: optional minus, digits, optional fraction, optional exponent. It says nothing about size or precision. A document containing 123456789012345678901234567890 is valid JSON.
What the RFC does do, in its section on numbers, is warn that implementations commonly use IEEE 754 binary64 (a "double"), and that only integers in the range from -(2^53)+1 to (2^53)-1 are interoperable in the sense that every such implementation will agree on their exact value. That range is exactly JavaScript's Number.MAX_SAFE_INTEGER: 9007199254740991.
So "valid JSON" and "a number every consumer will read identically" are two different things. The spec allows the first and only guarantees the second inside a 53-bit window.
A double has 64 bits: 1 sign bit, 11 exponent bits and 52 fraction bits (plus an implicit leading 1, giving 53 bits of significand). Every integer up to 2^53 fits exactly. Above that, the exponent has to grow, and each step of the exponent doubles the gap between neighbouring representable values.
| Integer range | Gap between representable values | Max rounding error |
|---|---|---|
| up to 2^53 (about 9.0 × 10^15) | 1 | 0 (exact) |
| 2^53 to 2^54 | 2 | 1 |
| 2^54 to 2^55 | 4 | 2 |
| 2^60 to 2^61 (about 1.2 to 2.3 × 10^18) | 256 | 128 |
| 2^63 (signed 64-bit max) | 2,048 | 1,024 |
The third row is why this bites so often in practice. Snowflake-style IDs (a millisecond timestamp, a worker ID and a sequence number packed into 63 bits) used by X, Discord and many internal ID services sit in the 2^60 to 2^61 range today. Every one of them is outside the safe window, and the rounding error is large enough that two different IDs routinely collapse onto the same double.
const a = JSON.parse('1861234567890123456')
const b = JSON.parse('1861234567890123460')
a === b // true: two distinct IDs, one number
That is the worst kind of failure. A crashed parser gets noticed. A parser that returns a neighbour's ID gets you a "user sees someone else's record" incident, or a 404 on an object that exists, or a cache that merges two keys.
The rounding is not a JSON problem so much as a "which type does the decoder pick" problem, and that differs by ecosystem.
| Runtime | Default for a large integer | Exact option |
|---|---|---|
JavaScript JSON.parse | double, silently rounded | reviver with context.source, or a lossless parser library |
Python json | arbitrary-precision int, exact | already exact; parse_float=Decimal for decimals |
Go encoding/json into interface{} | float64, silently rounded | decode into int64 fields, or Decoder.UseNumber() |
Go into a typed int64 field | exact | add the ,string struct tag to read and write it as a quoted number |
| Java Jackson | long or BigInteger by size, exact | USE_BIG_DECIMAL_FOR_FLOATS for decimals |
jq 1.6 and earlier | double, rounded | jq 1.7 preserves large integer literals it does not modify |
The pattern: statically typed decoders that know the target field is an int64 get it right; anything that decodes into a generic "any" type tends to pick a double. Go's documentation is explicit that numbers unmarshalled into an interface value become float64, and Python's shows the opposite default. The backend that produced the payload usually belongs to the exact group, which is why the bug survives every server-side test and shows up only in the browser, in a log pipeline, or in a quick script someone wrote against the API.
The robust answer is a wire contract, not a parser setting, because you do not control every consumer.
X hit this early and added an id_str field next to every numeric id; their guide to IDs still tells JavaScript clients to use the string form. Protocol Buffers made it the rule: the proto3 JSON mapping encodes int64, uint64 and fixed64 as JSON strings, and parsers accept either form on input. The IETF's I-JSON profile (RFC 7493) says the same thing in standards language: do not send numbers with more magnitude or precision than a double holds, and encode such values as strings.
In practice that means:
bigint in the database. Clients should never do arithmetic on an ID, so losing the numeric type costs nothing. It also frees you to change ID schemes later, a point that comes up again in this comparison of UUIDv4, UUIDv7 and ULID as primary keys."type": "string", "pattern": "^[0-9]+$", and the details of combining constraints like that are covered in JSON Schema in practice. In OpenAPI, use type: string with a description, rather than type: integer, format: int64, which generated JavaScript clients map straight to number. That gap between spec and generated code is a recurring theme in spec-driven OpenAPI development."id": 123 keep working while new ones send "id": "123".Sometimes you are the consumer of someone else's API, and the IDs arrive as bare numbers. In JavaScript you now have a native escape hatch. The JSON.parse source text access proposal gives the reviver a third argument with the original source text of each primitive, and adds JSON.rawJSON for the reverse direction:
// Read: keep the exact digits for any integer outside the safe range
const data = JSON.parse(text, (key, value, ctx) =>
typeof value === 'number' && !Number.isSafeInteger(value) && ctx && /^-?\d+$/.test(ctx.source)
? BigInt(ctx.source)
: value
)
// Write: emit a BigInt as a bare JSON number, not a string
JSON.stringify({ id: JSON.rawJSON(String(data.id)) })
// '{"id":1861234567890123456}'
Without rawJSON, JSON.stringify throws "Do not know how to serialize a BigInt", which is at least loud. Support is recent: Node 21 and later and current V8-based browsers ship both features, so check the compatibility table on MDN's JSON.parse page before relying on it in a browser bundle. Libraries such as json-bigint and lossless-json cover older runtimes by tokenizing numbers themselves.
Whatever you pick, convert at the boundary. A BigInt that leaks into the rest of a JavaScript codebase causes its own errors (1n + 1 throws a TypeError), so the usual move is to turn the exact value into a string immediately and keep IDs as strings everywhere after that.
Integers are the loud case because IDs must be exact, but fractional values go through the same double. 0.1 + 0.2 is 0.30000000000000004, and a price like 19.99 is stored as the nearest binary fraction, not as 19.99. A JSON number carrying money is therefore exact only for consumers that parse into a decimal type. The two common contracts are integer minor units ("amount": 1999, "currency": "USD", which keeps you far below 2^53 for any realistic amount) or a decimal string ("amount": "19.99"). Stripe-style APIs use the first; many banking and accounting APIs use the second. Both are better than a bare float.
The cheapest test is a fixture with an ID above 2^53 that round-trips through every client you ship: browser, mobile, CLI, and the "quick script" consumers in your docs. If any of them prints a different ID, the contract is wrong. When debugging a payload by hand, a strict scanner that reads the number tokens before they become doubles will show you which values are unsafe; the Utilix JSON Formatter flags integers outside the safe range for exactly this reason, since JSON.parse itself never will.
JSON numbers are arbitrary precision on paper and IEEE 754 doubles in most of the code that reads them. Anything that must be exact and can exceed 9,007,199,254,740,991, which includes every 64-bit ID, every nanosecond timestamp and most money, should cross the wire as a string, be declared as a string in your schema, and only become a number again inside code that has a type able to hold it.
Free, in your browser, no sign-up.
One email when a new post goes up. No other mail; unsubscribe in one click.
We store your email only to send new posts. See the privacy policy.