JSON is easy to read when it is formatted and painful to debug when it is not. A single missing bracket can break an API request, and a trailing comma copied from JavaScript can waste a surprising amount of time. This guide covers the rules that make JSON valid, how to choose indentation, and when to format, minify or sort keys.

Why formatting matters

Formatting adds indentation and line breaks without changing the data. It makes payloads easier to scan, review and diff. It also helps you spot structural problems, such as an array where an object was expected, before they reach production.

The rules that make JSON valid

JSON is stricter than JavaScript object syntax:

  • Keys and strings must use double quotes.
  • Keys must be quoted.
  • Trailing commas are not allowed.
  • Comments are not part of standard JSON.
  • Values can be objects, arrays, strings, numbers, booleans or null.
  • Numbers cannot contain NaN, Infinity or hexadecimal literals.

If a formatter rejects your input, one of those rules is usually the cause.

Format, minify or sort keys?

Formatting adds whitespace for humans. Minifying removes whitespace for smaller payloads and faster transfer. Sorting keys recursively reorders object properties so diffs and comparisons are easier to review.

Use minified JSON for production requests and formatted JSON for debugging, logs and code review. Sorting is useful when comparing equivalent payloads produced by different systems, but it does not change values or array order.

Common errors

Trailing commas. Remove the comma after the last item in an object or array.

Single quotes and unquoted keys. JSON requires double quotes around strings and keys.

Comments. Standard JSON does not support comments. Use JSONC or YAML if you need them, then convert to strict JSON before sending it to an API.

Smart quotes and invisible characters. Text copied from a word processor can contain characters that look like quotes but are not valid JSON delimiters. Retype them or paste into a plain-text editor.

Syntax validation vs schema validation

Formatting validates syntax, not meaning. A payload can be valid JSON while still missing required fields or using the wrong types. JSON Schema describes the expected structure and can validate those rules separately. Use syntax validation to catch parser errors and schema validation when your application depends on a contract.

A practical workflow

  1. Paste the raw JSON into a formatter.
  2. Format it and fix any parser error.
  3. Enable sort keys if you need to compare equivalent payloads.
  4. Minify when you need a compact version for transport.
  5. Copy or download the result.

I built a free JSON Formatter that runs locally in the browser. It validates the input, supports 2- or 4-space indentation, sorts keys and minifies the result. No uploads and no account:

https://minifyr.com/json-formatter

If you want the longer version, including more examples and edge cases, read the full guide:

https://minifyr.com/guides/json-formatting-best-practices