Guides

JSON Formatting Best Practices for Developers

6 min read

JSON is everywhere in a developer’s workflow, from API payloads to config files. A few simple conventions make it easier to read, review and maintain — and keep your version-control diffs clean.

Be consistent with indentation

Pick one indentation style — most teams use two spaces — and apply it everywhere. Consistency matters more than the specific number. A formatter applied on save guarantees every file looks the same regardless of who edited it.

Sort keys for clean diffs

When JSON files live in version control, key order matters for diffs. If one tool writes keys in insertion order and another alphabetically, you get noisy diffs that obscure the real change. Sorting keys consistently means a diff only shows what actually changed.

Minify for transport, beautify for humans

Use beautified, indented JSON in files people read and edit. Use minified JSON when sending data over the network or storing it at scale, where every byte counts. Keep the two concerns separate: format for humans, minify for machines.

Validate before you commit

An invalid JSON file can break a build or a deploy. Validate config and data files before committing — a quick paste into a validator catches trailing commas and stray quotes before they reach CI.

Watch for duplicate keys

JSON objects are not supposed to contain duplicate keys, and parsers silently keep only one — usually the last. A duplicate key is almost always a mistake. The formatter on this site flags duplicates so you can resolve them deliberately.

Frequently asked questions

How many spaces should I indent JSON?

Two spaces is the most common convention and keeps deeply nested data compact, but four spaces or tabs are also fine. The important thing is to be consistent across your project.

Should I commit minified JSON?

Generally no. Commit beautified JSON so diffs are readable, and minify only as a build or transport step. Readable source files are easier to review and maintain.

Try the tools