Formatting JSON Safely

How to format and validate JSON without losing precision, comments, or trust.

Formatting JSON looks trivial until large integers, duplicate keys, or trailing content enter the picture. This article covers the safe path.

01Why precision matters

JavaScript numbers cannot represent every 64-bit integer exactly. A formatter that parses through IEEE-754 doubles can silently corrupt identifiers.

  • Validate before you reformat.
  • Keep large integers as strings when they are identifiers.
  • Reject trailing content instead of ignoring it.
jsonSelect to copy
{
  "orderId": "9007199254740993",
  "currency": "USD",
  "total": 12.5
}
Failure modes when formatting JSON
InputNaive resultSafe result
9007199254740993Corrupted integerPreserved string
Trailing commaSilently acceptedReported error
Duplicate keyLast value winsReported error

Frequently asked questions

Does formatting change my data?
A correct formatter only changes whitespace. If a value changes, the input was not valid JSON for that parser.
Should I minify before sending?
Minify for transport, keep a human-readable copy for review and diffs.

Sources

  1. RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format — IETF (accessed 2026-09-01)
  2. ECMA-404: The JSON Data Interchange Syntax — Ecma International (accessed 2026-09-01)