Serialize Large Integer IDs as Strings in JSON APIs
JSON numbers round silently in JavaScript beyond 2^53−1. Serialize large integer IDs as strings, define the contract at the schema boundary, and avoid common mistakes like mixed types or post-parse validation.
11 Jul 2026, 08:48 UTC

The short answer
When your API returns integer identifiers that can exceed 253−1 (9,007,199,254,740,991), serialize them as JSON strings. JavaScript and any other consumer that maps JSON numbers to IEEE 754 binary64 will silently round larger integers, turning 9007199254740993 into 9007199254740992. A string preserves every digit and makes the contract explicit: the field is an opaque identifier, not a number you do arithmetic on.
Why the rounding happens
JSON's grammar allows arbitrarily long digit sequences, but the spec does not mandate how a parser represents them. In JavaScript (and many other runtimes), JSON.parse converts a JSON number to a binary64 floating-point value. Binary64 has 53 bits of significand precision, so integers beyond 253−1 cannot all be represented exactly. The parser rounds to the nearest representable value.
// Node.js, browser console, or any JS runtime
JSON.parse('{"id":9007199254740993}').id === 9007199254740992
// true — the last digit was lost
This is not a JSON syntax error. The document is valid; the data loss occurs during parsing.
Worked producer example (JavaScript)
Convert the identifier to a string before it reaches JSON.stringify. The simplest safe pattern uses a BigInt literal and toString():
const recordId = 9007199254740993n; // BigInt literal
const payload = JSON.stringify({ id: recordId.toString() });
// payload === '{"id":"9007199254740993"}'
Do not pass a BigInt directly to JSON.stringify — it throws TypeError: Do not know how to serialize a BigInt.
If your IDs originate from a database or another system as decimal strings, keep them as strings throughout your service layer and skip the BigInt step entirely.
Contract guidance at the schema boundary
Define the field as a string in your OpenAPI, JSON Schema, or Protocol Buffers JSON mapping:
- Type:
string - Pattern:
^[0-9]+$(decimal digits only) - Maximum length: set a bound that matches your ID generator (e.g., 20 characters for 64-bit unsigned)
- Leading zeros: decide whether
"00123"and"123"are distinct and document the decision
Avoid accepting both strings and numbers in the same field unless you are executing a documented, time-boxed migration. Mixed-type acceptance hides the rounding bug in consumers that parse the numeric form.
Common mistakes
1. Stringifying only large IDs
// Inconsistent — breaks consumer type expectations
if (id > Number.MAX_SAFE_INTEGER) {
obj.id = id.toString();
} else {
obj.id = id; // number
}
Consumers must then handle string | number, and the numeric branch still rounds for any value above 253−1.
2. Validating after a lossy parse
const data = JSON.parse(responseText); // rounding already happened
if (data.id !== expectedId) { /* mismatch */ }
The check compares two already-rounded values and can pass incorrectly.
3. Calling parseInt on the decoded string
const id = parseInt(data.id, 10); // back to binary64, rounding returns
If you need arithmetic, convert to an explicit arbitrary-precision type (e.g., BigInt in JS, decimal.Decimal in Python, big.Int in Go). Compare opaque IDs as strings.
When JSON numbers are still appropriate
This guidance applies to opaque identifiers. For genuine numeric quantities — prices, measurements, counters — JSON numbers remain the right choice when the contract bounds their range and precision. String encoding alone does not define decimal arithmetic or rounding rules; financial values need a separate semantic layer (fixed-point, decimal type, or minor-unit integers).
Interoperability notes
- The 53-bit limit describes JavaScript-style binary64 handling. Other parsers (Python's
json, Go'sencoding/jsonwithUseNumber, Rust'sserde_json) can preserve larger integers exactly or expose them as arbitrary-precision types. Check each integration's actual parser. - Changing an established numeric ID field to a string is a wire-format breaking change. Coordinate producers and consumers or version the contract (e.g.,
id_v2or a new API version). - Opaque alphanumeric IDs (UUIDs, KSUIDs, prefixed codes) can simply remain strings without numeric validation.
Verification checklist
- Run the worked
JSON.parseexpression in the target JavaScript runtime and confirm the rounding behavior. - Parse the string-encoded form and assert that its digits survive unchanged.
- Exercise producer and consumer with boundary values: 9007199254740991 (max safe), 9007199254740992 (first rounded), 9007199254740993 (first lost). Verify both the serialized JSON type and lossless round-trip.
- Check each integration's parser and serializer for maximum length, accepted characters, leading-zero policy, and rejection of unintended mixed string/number representations.
Limitations
String IDs increase payload size slightly and prevent native numeric sorting in databases that rely on JSON number ordering. If your IDs are guaranteed to stay within 53 bits (e.g., auto-incrementing 32-bit or 48-bit integers), the string conversion is unnecessary overhead. Apply the pattern only when the identifier space can exceed the consumer's safe-integer range.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.