Fixing 64-Bit Snowflake ID Precision Loss in JSON APIs & Formatters
Why do 19-digit order IDs silently turn into trailing zeros after formatting? Learn the IEEE 754 root cause, backend fixes, and lossless debugging workflow.
The Symptom: Trailing Digits Silently Rounded
Backend microservices frequently generate 64-bit Snowflake IDs (19 decimal digits, e.g. 1876543210987654321). When serialized as a JSON number instead of a string, any browser JSON.parse() call or naive online JSON beautifier silently rounds the lowest digits—breaking database lookups and audit trails.
{
"orderId": 1876543210987654321,
"payAmount": 199.50,
"status": "PAID"
} {
"orderId": 1876543210987654400,
"payAmount": 199.5,
"status": "PAID"
} Root Cause: IEEE 754 Double-Precision Limit
JavaScript numbers are IEEE 754 64-bit floats whose safe integer ceiling is Number.MAX_SAFE_INTEGER (2^53 - 1 = 9007199254740991, 16 digits). Anything larger loses least-significant bits on parse, while decimal literals like 199.50 also lose trailing zeros.
Production Fix & Safe Debugging Workflow
Serialize 64-bit identifiers as JSON strings at the API boundary. During incident triage, always inspect raw payloads with a token-preserving formatter rather than a JSON.parse-based tool.
public class OrderDTO {
@JsonSerialize(using = ToStringSerializer.class)
private Long orderId;
// Prefer BigDecimal serialized as string or fixed scale for currency
private BigDecimal payAmount;
} - Register ToStringSerializer globally on your ObjectMapper for Long.class / Long.TYPE to prevent per-field omissions.
- Check the raw Response tab in browser DevTools rather than the Preview tab, which parses via JS Number.
- ITGrocery Fmt uses a token-preserving formatter and lossless-json so 19-digit IDs and 199.50 decimals remain byte-for-byte accurate.
Quick Verification Checklist
- Are all 64-bit IDs (Snowflake IDs, trace IDs, transaction IDs) serialized as strings in JSON APIs?
- Are you using a lossless formatter instead of a native JSON.parse tool when inspecting production logs?
- Do decimal values like 199.50 retain exact precision during formatting and key sorting?
Format Large-Integer JSON Losslessly
Runs 100% locally in a browser Web Worker—preserving 64-bit integers, trailing-zero decimals, and JSONPath lookup.