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.
Not a schema validator
Syntactic validity is not semantic correctness. Validate the shape of the data against your own schema.
{
"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)
- ECMA-404: The JSON Data Interchange Syntax — Ecma International (accessed 2026-09-01)
Test this in your browser
The tool runs entirely in your browser; the input you use never leaves it.
About the author
Gampangin builds and maintains free, browser-based developer tools and the documentation that makes them trustworthy. The organization is accountable for every page published on this site.