Minifyr

Data & JSON

JSON Formatting Best Practices

A practical guide to readable, valid JSON for APIs, configuration files and debugging.

By Minifyr Team · 2026-11-02 · 4 min read

Why JSON Formatting Matters

JSON is designed to be read by machines, but developers spend a lot of time reading it too. A formatted JSON document is easier to scan, review and debug than a single long line of text. Formatting adds indentation and line breaks without changing the data, so it is safe for API payloads, configuration files and test fixtures.

Good formatting is not just aesthetics. It makes diffs meaningful, reduces the chance of missing a bracket or comma during review, and helps you spot structural problems such as an array where an object was expected.

Valid JSON Rules

A formatter can only format valid JSON. The JSON grammar is stricter than JavaScript object syntax:

  • Keys and strings must use double quotes, not single quotes.
  • Keys must be quoted; unquoted property names are invalid.
  • Trailing commas are not allowed in objects or arrays.
  • 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 the formatter rejects your input, the error is usually one of those rules. Fix the syntax first, then format.

Indentation and Style

Two spaces are common in JavaScript and web projects; four spaces are common in other ecosystems. Tabs are valid too, but mixing tabs and spaces in the same repository makes diffs noisy. The exact width matters less than consistency. Choose a style, configure your editor and formatter, and keep it stable.

Use UTF-8 encoding and normal double quotes. Avoid smart quotes copied from word processors; they are not valid JSON delimiters. If you paste from a document and see unexpected characters, retype the quotes or convert the text to plain text first.

Format, Minify and Sort Keys

Formatting adds whitespace for humans. Minifying removes that whitespace for smaller payloads and faster transfer. Sorting keys recursively reorders object properties so comparisons and diffs are easier to review. Use minified JSON for production requests and formatted JSON for debugging, logs and code review.

Sorting keys is useful when you compare two payloads that should be equivalent but were produced by different systems. It is not a substitute for semantic comparison, because arrays keep their order and sorting does not change values.

Common Syntax Errors

Trailing commas

A comma after the last item is a common JavaScript habit but invalid JSON. Remove the final comma before the closing brace or bracket.

Single quotes and unquoted keys

JSON requires double quotes around strings and keys. JavaScript object literals are more permissive, so a value that works in code may fail when parsed as JSON.

Comments

Standard JSON does not support comments. If you need comments, use a format that supports them, such as JSONC or YAML, and convert to strict JSON before sending it to an API that expects JSON.

Unexpected characters

A byte-order mark, non-breaking spaces, smart quotes or invisible control characters can cause parse errors. Paste into a plain-text editor or retype the affected section.

JSON in APIs and Configuration

APIs usually send minified JSON to reduce payload size. Configuration files are easier to review when formatted. A practical workflow is to keep production requests compact, then paste a captured response into a formatter to inspect its structure. For configuration changes, format the file before committing so diffs show real changes instead of whitespace noise.

Do not paste production secrets, private keys or personal data into online tools unnecessarily. Minifyr formats locally in your browser, but it is still good practice to minimize exposure of sensitive values.

Syntax Validation vs Schema Validation

Formatting validates JSON 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. The two checks solve different problems.

A Practical Workflow

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

When the JSON contains encoded segments, use the Base64 Encoder / Decoder or the JWT Decoder to inspect them, then return here to format the surrounding payload.

Frequently asked questions

Should I use two or four spaces?

Two spaces are common in JavaScript projects and four spaces in other ecosystems. Consistency across the repository matters more than the exact width.

Does formatting change the JSON data?

No. Formatting only changes whitespace that is insignificant to a JSON parser. The values and structure stay the same.

Can I format invalid JSON?

No. The formatter validates the input first and reports a parser error. Fix the syntax before formatting.

Tools used in this guide

Related guides