Data Integrity
JSON Large Integers: Preserve IDs and Exact Values
Prevent JSON large-integer precision loss by defining string IDs, exact decimal contracts and round-trip tests across JavaScript, databases and exports.
In this article
JSON Large Integers: Preserve IDs and Exact Values
A JSON number can be valid syntax and still lose precision when a client parses it. This often appears as a changed identifier, an incorrect financial amount or two distinct values that become equal in JavaScript. Fix the representation contract before adding more formatting or retry logic.
The key question is whether a field represents a quantity that needs arithmetic or an identifier whose characters must stay exact. Treat those cases differently. A database's ability to store a large integer does not guarantee that every consumer can safely handle it as a JSON number.
Identify the parsing boundary
JavaScript's ordinary Number representation cannot represent every integer beyond its safe integer range exactly. A response can arrive with the correct characters and be rounded during parsing. By the time a normal parsed-object comparison runs, the original distinction may already be gone.
The JSON.parse reference discusses precision concerns and alternatives for preserving large numeric values. Investigate the parser and runtime used by each consumer rather than assuming JSON itself dictates one numeric storage type.
An illustrative risky payload is:
{"external_id":9007199254740993}For an identifier, prefer a contract such as:
{"external_id":"9007199254740993"}The string form preserves the identifier's characters without requiring clients to perform arithmetic on it.
Audit fields by meaning
List external IDs, account numbers, sequence numbers, money, measurement values and timestamps. Record the required range and precision for each field. A small count and a nineteen-digit identifier should not share a representation merely because both look numeric.
Leading zeros are another reason to use strings for identifiers. The value 001245 may carry formatting or identity information that ordinary numeric conversion removes. Phone numbers and postal codes are also generally not arithmetic quantities.
For money, define the currency and scale. Integer minor units can work if the maximum range is safe across all clients. A decimal string can preserve exact values for larger or variable-scale amounts, provided the server uses an appropriate decimal arithmetic implementation.
Inspect raw data before formatting
Keep a synthetic raw-response fixture and its parsed representation. Compare them at the boundary where the value changes. Don't assume that a pretty-printed response is the original response text.
The JSON Viewer and JSON Formatter are useful for structure review. However, browser-based JSON tools can share the runtime's numeric limitations. Use string-based examples for exact large IDs and inspect the raw fixture with a text editor when testing unsafe numeric values.
This is an important limit: formatting cannot recover digits that a parser has already rounded. A displayed value that looks clean may be precisely the wrong value.
Define serialization explicitly
JavaScript BigInt can represent large integers, but it isn't automatically a portable JSON number contract. Decide how your serializer maps it, commonly to a decimal string for interoperable API output.
Keep the schema aligned with that choice. If an ID is a string, client types, examples and validators should all say string. An interface that alternates between numbers and strings based on magnitude forces consumers to guess.
Don't silently coerce every numeric-looking string into a number during import. Limit conversion to fields whose documented meaning requires it. Otherwise, a cleaning step can corrupt identifiers before the database sees them.
Test the complete round trip
Create values just below, at and above the consumer's precision boundary. Include a leading-zero identifier, a negative value where allowed and a decimal amount with trailing zeros. Use synthetic data, not real account identifiers.
Run each value through database read, API serialization, client parse, display, edit submission and database write. Compare the required exact representation at each stage. Add CSV export and spreadsheet import if those are supported product paths.
Use CSV Viewer to review a synthetic export's field layout. A spreadsheet program may perform its own type inference when opening the file, so test the actual target software separately.
Migrate without mixing representations unpredictably
If an existing numeric ID field must become a string, treat it as an API compatibility change. Update generated clients, validation rules and storage assumptions. Consider a transition field or versioned response if old clients cannot accept the new representation.
Check sorting and filtering. Lexical string order differs from numeric order for differently sized numeric strings. Preserve the intended database ordering rather than letting a frontend sort IDs as quantities by accident.
Review cache keys and deduplication logic too. If two large IDs were previously rounded to the same value, correcting the representation may uncover collisions in persisted client state. Repair those records deliberately rather than hiding the symptom with a cast.
Keep calculations in the right layer
For exact financial operations, use the application's chosen integer or decimal domain on the server and define rounding rules. Display formatting should happen after the exact value is established. Don't convert through a floating-point intermediate just to insert a currency symbol.
Document whether trailing zeros matter as presentation, scale or identity. These are separate requirements and should not be inferred from whatever a serializer happens to emit.
Conclusion
Preserve exact JSON values by choosing representations according to meaning. Use strings for large identifiers, define money and decimal contracts explicitly, and test every serialization boundary. Valid JSON is only the start; the application must keep the value unchanged throughout its journey.