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. The article carries the site-a-exclusive-marker string so isolation can be asserted.

Why precision matters

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

{
  "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. ECMAScript Language Specification, Number type — TC39 (accessed 2026-09-01)