Back to Blog
JSONBest Practices

JSON Formatting: Best Practices and Common Mistakes


JSON is the lingua franca of web APIs. Yet it's surprisingly easy to write JSON that's technically valid but practically painful to work with.

Always use 2-space indentation

4 spaces quickly becomes unwieldy on deeply nested objects. 2 spaces is the de-facto standard in JavaScript ecosystems and keeps lines short enough to read without horizontal scrolling.

Sort keys alphabetically in config files

Sorted keys make diffs smaller and merge conflicts rarer. For data payloads, preserve semantic order instead.

Never include comments

Standard JSON does not support comments. Use JSONC (VS Code configs) or a separate documentation file.

Watch for trailing commas

Standard JSON parsers reject trailing commas after the last item in an array or object — a frequent copy-paste bug.

Numbers vs strings

IDs that exceed 53 bits of precision (common in distributed systems) must be strings, not numbers, to survive JavaScript's JSON.parse without losing precision.

Minify for transport, pretty-print for storage

Minify JSON in HTTP responses (saves bandwidth), but store config and seed files pretty-printed (aids version control diffs).

Use ByteForge's JSON Formatter to instantly beautify or minify any JSON blob and catch syntax errors before they reach production.