Skip to content

FIELD NOTES / A DECISION GUIDE

Why Large Integer IDs Change When You Parse JSON

On this page
AI-generated illustration; not taken on location · Conceptual flow showing an integer's final digit changing during Number parsing and remaining changed in serialized output.
AI-generated illustration; not taken on location · Precision can be lost during conversion from source text to a runtime number.AI-generated illustration

Large integer IDs can change because parsing converts their written digits into a numeric representation that cannot preserve every integer exactly. When a JSON number becomes a JavaScript Number, precision may already be lost before indentation, minification, or output serialization begins. Successful JSON validation does not guarantee that an application will retain the original identifier.

The right solution depends on what you need to preserve. If you control the producer, emitting an identifier as a string can keep its original digits available to consumers. If you only need to format JSON text, retaining the original numeric tokens can preserve those digits without treating them as runtime numbers. If the application needs exact integer values, its parser and subsequent processing must use an exact representation.

This guide supports decisions about JSON workflows in FormatBits. It explains what to inspect and test; it does not establish which parsing or preservation strategy FormatBits currently implements.

Where the digits change

Start by separating the JSON source text from the value an application holds after parsing. The source contains a written numeric token. A parser that converts that token into JavaScript Number must fit its value into JavaScript’s fixed numeric precision. For some large integers, the resulting value differs from the integer written in the input.

Consider this identifier:

{"id":9007199254740993}

When parsed into JavaScript Number, that numeric text produces 9007199254740992. If a later serialization step writes the resulting numeric value, the output carries the changed identifier. Adding whitespace cannot restore the missing distinction.

Stage Identifier representation What to inspect
Input text 9007199254740993 The original numeric token
Parsed Number 9007199254740992 The value held by the application
Serialized output 9007199254740992 The digits written from that value

This explains why a formatter can appear responsible even when the decisive conversion happened earlier. The formatted output is where the change becomes visible, but the first loss may have occurred during parsing.

What successful validation tells you

Keep syntax acceptance and numeric preservation as separate checks. JSON can contain numeric literals whose precision exceeds JavaScript’s numeric representation. A payload can therefore be accepted as JSON while one of its numbers cannot survive conversion to Number exactly.

The useful debugging question is: at which step does the original token first differ from the application’s value? Capture the input text and examine the parsed representation before investigating indentation or minification.

Why large integers expose numeric precision limits

JavaScript Number has fixed precision. It preserves every positive integer through 9007199254740991, but that guarantee does not extend to every larger integer. This is a boundary for a continuous range of exact positive integers, not a claim that all integers above it are impossible to represent.

That distinction matters when testing an identifier pipeline. A single large ID may survive parsing, giving the impression that the entire range is safe. A nearby ID may then change. Successful handling of one value does not establish that adjacent identifiers will remain distinct.

Test a neighborhood around the boundary

Use a small collection of original digit strings as test fixtures:

9007199254740990
9007199254740991
9007199254740992
9007199254740993

Retain those fixtures as text so the test’s expected values do not themselves pass through the conversion under investigation. Then submit the corresponding numeric JSON fields through the actual processing path and compare the results with those original strings.

The important result is whether every intended identifier remains distinguishable. In the example above, parsing 9007199254740993 as Number yields 9007199254740992. Distinct original integers can therefore share a floating-point representation.

Before choosing a remedy, decide what the field means. Does it identify a record, or does it represent a quantity requiring arithmetic? For an identifier, preserving its exact identity is usually the immediate requirement. If calculations are required, establish an exact numeric representation and verify the operations that will use it. Merely increasing the variety of test IDs does not replace that design decision.

Use string IDs at the producer

If you control the producer, consider emitting the original exact identifier as a JSON string. Compare these two representations:

{"id":9007199254740993}
{"id":"9007199254740993"}

The quoted form allows a recipient to handle the identifier as text without needing high-precision numeric support. The crucial condition is that the producer creates the string from an exact source. It must not first convert the identifier into a rounded Number and then quote the result.

Passing a string to JSON.stringify produces a JSON string. This makes the type choice visible in the output: quotation marks communicate that the field is being carried as text. JCS also recommends JSON strings for applications requiring longer integers or higher precision than double precision offers.

Agree on the field type with consumers

Changing an existing field from a number to a string requires coordination. Ask consumers whether they expect text, perform arithmetic, convert the field into Number, or validate it against a numeric field requirement. A string at the producer protects the digits only while subsequent steps continue to preserve them.

Document the intended contract with a concrete example, including the quotation marks. Ask each consumer to demonstrate that it accepts and returns the same identifier without an intermediate numeric conversion.

This approach is especially useful when the field’s purpose is identification and consumers can work with text. If a consumer genuinely needs exact arithmetic, it should deliberately convert the original string into an appropriate exact representation rather than treating the string change as the whole solution.

Preserve numeric tokens when formatting

When the task is indentation or minification, ask whether the formatting path retains each original numeric token. A workflow that preserves that text can keep 9007199254740993 intact while changing the surrounding whitespace. It does not need to replace the token with a Number-derived rendering to achieve that formatting goal.

This is a different requirement from making the number available for calculations. Retaining the written token preserves the supplied digits for output; it does not establish that application code can perform exact arithmetic on the value.

Requirement Preservation approach to evaluate Verification question
Change whitespace while keeping digits Retain original numeric tokens Does the output contain the same token?
Carry an identifier as text Use a string from the exact source Do consumers preserve the string?
Compute with an exact integer Parse into an exact integer representation Do operations and output retain the value?

Check the formatting path explicitly

For a FormatBits workflow, use a known boundary example and compare input with output. Also inspect the implementation or its documented behavior where available. A tool’s acceptance of the input does not establish token preservation, and its ability to format ordinary numbers does not settle the large-integer case.

If your requirement is to preserve the exact written digits, state that explicitly. If your requirement permits a different numeric notation that denotes the same exact value, state that instead. Those are different acceptance criteria, and a useful formatting test should identify which one it checks.

Parse exact integers when the producer cannot change

When numeric JSON fields must remain numeric, evaluate a parser that preserves their exact values. Check what representation it returns, how application code accesses that representation, and how the result will be serialized. Calling a parser “lossless” is not a substitute for verifying the behavior your field needs.

A reviver offers another possible route, but its inputs matter. The numeric value argument arrives after parsing and may already have lost precision. Building an exact integer from that rounded argument cannot recover the original identifier.

The documented context.source property provides the original source text for a primitive value. A documented reviver example uses that source text to construct a BigInt instead of using the rounded numeric value. For an agreed integer field, the pattern is:

const parsed = JSON.parse(input, (key, value, context) => {
  if (key === "id" && typeof value === "number") {
    return BigInt(context.source);
  }
  return value;
});

This example assumes that the field contract supplies integer text suitable for the conversion and that the target runtime supports the required source context. Establish both conditions before adopting it. Avoid applying an integer conversion indiscriminately to every numeric field.

Plan serialization before choosing the representation

JSON.stringify does not automatically serialize BigInt as a JSON number. Decide how exact values will leave the application before treating parsing as solved. If the output contract permits strings, agree on that representation. If the output must contain numeric tokens, verify that the chosen output mechanism writes the exact value without routing it through Number.

Why converting the rounded value cannot repair it

Converting the original digit string and converting an already rounded Number start from different information. The original text still contains the intended identifier. The rounded Number may no longer distinguish it from another integer.

Compare these paths:

Original text: "9007199254740993"
  → retain as text
  → "9007199254740993"

Numeric token: 9007199254740993
  → parse as Number
  → 9007199254740992
  → convert to string
  → "9007199254740992"

The final string in the second path faithfully records the value available at that point. It does not restore the input. Adding quotation marks after precision loss changes the representation of the rounded value, while the discarded distinction remains unavailable.

The same limitation applies when creating an exact integer representation from the rounded Number. An exact destination cannot supply information that its input has already lost. Recovery requires an independent exact representation, such as the original digit string or an accessible original numeric source token.

Review fixes at the point where information is lost

If a proposed fix says “convert the ID to a string,” ask where that conversion occurs and what it reads. Converting the producer’s exact identifier can prevent loss. Converting the parser’s rounded Number cannot repair it.

If only the rounded value remains, avoid guessing the original final digits. Return to an exact source and repeat the affected processing path with preservation in place. Otherwise, the system may consistently store and return a plausible-looking identifier that differs from the one supplied.

Check the complete identifier round trip

A reliable check follows the identifier from original text to final output. Testing only the formatter’s visible result can miss a conversion in an earlier layer, while testing only parsing can miss a later step that converts an exact value back into Number.

Use this sequence for the actual application path:

  1. Save the original identifier digits as an exact text fixture.
  2. Inspect the submitted JSON token and whether the field is quoted.
  3. Check the parser’s returned representation before transformations begin.
  4. Review transformations for conversions into Number.
  5. Verify what is written to storage and what is read back.
  6. Serialize through the real output path and compare with the fixture.

Include adjacent values around 9007199254740991, especially 9007199254740992 and 9007199254740993. Confirm that distinct input identifiers remain distinct through parsing, processing, storage, and reserialization. Test both the numeric field contract and any proposed string replacement so the comparison reflects the migration you intend to make.

Define what a passing result means

For token-preserving formatting, compare the original numeric token with the output token. For exact numeric processing, establish whether the exact integer value survived, even if your contract permits its written notation to change. If exact digits are part of the requirement, a notation change also needs attention.

Finish the review by answering three questions: where is the exact representation retained, which operations can change it, and how is it written out? For FormatBits users, those questions provide a concrete way to evaluate a workflow without assuming unverified product behavior. The complete path must preserve the requirement, from the producer’s original identifier to the final consumer.