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.
- Validate before you reformat.
- Keep large integers as strings when they are identifiers.
- Reject trailing content instead of ignoring it.
{
"orderId": "9007199254740993",
"currency": "USD",
"total": 12.5
}| Input | Naive result | Safe result |
|---|---|---|
| 9007199254740993 | Corrupted integer | Preserved string |
| Trailing comma | Silently accepted | Reported error |
| Duplicate key | Last value wins | Reported 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
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format — IETF (accessed 2026-09-01)
- ECMAScript Language Specification, Number type — TC39 (accessed 2026-09-01)