Javascript is required
PitfallsPublished 2026-07-2814 min read

Why Large JSON Numbers Lose Precision: JavaScript Number Limits Explained

In web development, numeric identifiers such as order numbers, Snowflake IDs, credit card numbers, and 64-bit database primary keys often suffer from silent digit changes once parsed by JSON.parse in JavaScript. This is not a flaw in JSON specification itself, but a consequence of JavaScript's Number type being implementation-tied to the IEEE 754 standard. This guide explains the root mechanics and provides production-ready solutions.

JSONJavaScriptNumberBigInt精度丢失

1. Where Precision is Lost: IEEE 754 Double Precision Mechanics

JavaScript's Number type is a double-precision 64-bit binary format IEEE 754 value. Out of 64 bits, 1 bit is assigned to sign, 11 bits to exponent, and 52 bits to fraction (significand). Because binary representation cannot exactly represent all decimal integers beyond 52 bits of precision, the maximum safe integer in JavaScript is limited to 2^53 - 1, which is 9,007,199,254,740,991 (Number.MAX_SAFE_INTEGER).

When 64-bit integers from backends (such as Java's long or Go's int64) exceed this limit, the JS parsing engine rounds the value to the nearest representable floating-point number. As a result, adjacent odd and even integers get mapped to the same number, truncating the least significant digits.

Crucially, RFC 8259 (the JSON specification) puts no constraint on numeric literal length or precision. The incoming raw JSON text is completely valid; the corruption occurs entirely at the exact instant JavaScript deserializes it into memory.

robust handling of Why Large JSON Numbe and optimal network throughput. Technical teams must establish automated regression testing pipelines, linting rules, and strict monitoring alerts to detect anomalies early in the development lifecycle.

When evaluating tech stacks or refactoring systems, measure key performance metrics like Time-to-Interactive (TTI) and Main Thread Blocking (TBT) to make data-driven architecture decisions.

  • Establish automated regression test suites covering boundary edge cases.
  • Define strict naming and typing conventions across cross-team API contracts.
  • Monitor production error logs continuously to catch anomalies proactively.
// Safe integer threshold
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991

// Precision corruption demo
const rawJson = '{"orderId": 9007199254740993, "snowId": 1819283748593829183}';
const parsed = JSON.parse(rawJson);

console.log(parsed.orderId); // 9007199254740992 (The last digit 3 turned into 2)
console.log(parsed.snowId);  // 1819283748593829000 (Lower digits truncated to zeros)

2. Disaster Scenarios in Full-stack Applications

Precision loss causes silent yet catastrophic bugs in production:

1. UI & Customer Support Confusion: Order numbers displayed on screen mismatch real backend database records.

2. Destructive API Calls: A frontend app fetches a parsed ID (corrupted), then sends it back to DELETE /api/user/1819283748593829000, causing the backend to delete an unintended neighbouring record!

robust handling of Why Large JSON Numbe and optimal network throughput. Technical teams must establish automated regression testing pipelines, linting rules, and strict monitoring alerts to detect anomalies early in the development lifecycle.

When evaluating tech stacks or refactoring systems, measure key performance metrics like Time-to-Interactive (TTI) and Main Thread Blocking (TBT) to make data-driven architecture decisions.

  • Snowflake IDs: Typically 64-bit integers (e.g., 1819283748593829183) that always exceed Number.MAX_SAFE_INTEGER.
  • Financial Ledger Cent/Mill Amounts: High-precision monetary numbers.
  • Distributed Tracing IDs: TraceIDs represented as pure numeric sequences.

3. Three Architectural Solutions

The golden rule is: Never represent pure identifiers as JS Number values. Here are three layer-by-layer architectural approaches:

1. Solution 1 (Recommended Best Practice - Contract Layer Stringification): Configure backend serializers to convert 64-bit numbers into JSON strings. For example, in Java Jackson, use @JsonSerialize(using = ToStringSerializer.class). The JSON payload becomes {"id": "1819283748593829183"}, which JS parses losslessly as a String.

2. Solution 2 (Frontend Parse Interception): When backends cannot be altered, avoid standard JSON.parse on raw HTTP response texts. Use libraries like json-bigint to parse big integers into native BigInt or BigNumber objects.

3. Solution 3 (BigInt Serialization Patch): If native BigInt is used for calculations in JS, JSON.stringify(BigInt(123)) throws TypeError: Do not know how to serialize a BigInt. Always define BigInt.prototype.toJSON.

robust handling of Why Large JSON Numbe and optimal network throughput. Technical teams must establish automated regression testing pipelines, linting rules, and strict monitoring alerts to detect anomalies early in the development lifecycle.

When evaluating tech stacks or refactoring systems, measure key performance metrics like Time-to-Interactive (TTI) and Main Thread Blocking (TBT) to make data-driven architecture decisions.

  • Establish automated regression test suites covering boundary edge cases.
  • Define strict naming and typing conventions across cross-team API contracts.
  • Monitor production error logs continuously to catch anomalies proactively.
// Solution 2: Custom JSON Parser (json-bigint)
import JSONbig from 'json-bigint';

const rawJson = '{"snowId": 1819283748593829183}';
const data = JSONbig({ storeAsString: true }).parse(rawJson);
console.log(data.snowId); // "1819283748593829183" (Safely preserved as string)

// Solution 3: BigInt JSON Serialization
BigInt.prototype.toJSON = function() {
  return this.toString();
};
console.log(JSON.stringify({ id: BigInt("1819283748593829183") }));
// Output: '{"id":"1819283748593829183"}'

Engineering Reality: Debugging and Defensive Coding

When diagnosing issues in production, first extract the raw network payload and exact stack trace. Reproduction failures locally are often caused by edge-case payloads containing unexpected null values, missing optional fields, or non-printable unicode BOM characters.

Adopt defensive programming habits. Validate runtime API data with Schema validation libraries (like Zod or TypeBox) and enforce pre-commit testing in CI/CD. Catching boundary errors at build time prevents emergency midnight hotfixes.

  • Extract raw production network payloads for local breakpoint debugging.
  • Use Zod or TypeBox schemas to validate external API responses.
  • Add automated regression test cases in CI/CD pipelines.

Keep exploring

Use the related tool to validate, format, or inspect your JSON directly in the browser.

Open tool