JavaScript
Serialize BigInt values deliberately
Convert BigInt values to decimal strings before JSON serialization. JSON.stringify does not serialize BigInt directly, and converting a large integer to Number can lose precision.
Worked example
Node.js example · save as example.mjs and run node example.mjs
const record = {id: 9007199254740993n};
const json = JSON.stringify(record, (_, value) =>
typeof value === "bigint" ? value.toString() : value
);
console.log(json);How it works
The replacer receives values as serialization proceeds and returns text for each BigInt. The full decimal digits survive the JSON round trip. This is appropriate for identifiers or counters whose exact integer representation matters more than treating them as ordinary JSON numbers.
Limits and mistakes
The receiving application needs a schema to know which strings represent integers. Automatically converting every digit-only string back to BigInt could corrupt account numbers or text with leading zeros. Document the conversion field by field.
Check the result
Expect {"id":"9007199254740993"}. Parse the JSON and confirm id is a string, then convert that known field with BigInt if the consumer needs integer arithmetic.
Sources
Reference documentation for the behavior used in this guide: