DevPad
← Back to blog
·json, formatting, api, developer-tools

How to Format JSON: A Complete Developer Guide

Learn how to format, validate, and minify JSON data. Covers syntax rules, common errors, and best practices for working with JSON in APIs and configuration files.

JSON (JavaScript Object Notation) is the most widely used data interchange format on the web. Every REST API, most configuration files, and countless data pipelines rely on JSON. Yet poorly formatted JSON is one of the most common sources of bugs and wasted debugging time.

Why JSON Formatting Matters

When you receive JSON from an API, it's often minified — all whitespace removed to reduce payload size. While this is fine for transmission, it makes the data nearly impossible to read or debug. A single missing comma in a 500-line minified response can take minutes to locate manually.

Properly formatted JSON solves this instantly. With consistent indentation (typically 2 or 4 spaces), the hierarchical structure becomes clear at a glance. You can quickly spot missing commas, trailing commas, mismatched brackets, and other syntax errors that cause parsing failures.

JSON Syntax Rules

Every JSON value must follow these rules:

  • Strings use double quotes: "hello" (not 'hello' or hello)
  • Numbers have no trailing dots: 42 not 42.
  • Booleans are lowercase: true and false
  • Null is lowercase: null
  • Objects use curly braces: {"key": "value"}
  • Arrays use square brackets: [1, 2, 3]
  • No trailing commas allowed after the last element
  • No comments — JSON does not support // or /* */

Common JSON Errors

The most frequent mistakes developers make with JSON:

  1. Trailing commas[1, 2, 3,] is invalid in standard JSON (though some parsers accept it)
  2. Single quotes{'key': 'value'} must be {"key": "value"}
  3. Unquoted keys{key: "value"} must be {"key": "value"}
  4. Mismatched brackets — forgetting to close { or [
  5. Comments — JSON does not support comments; use a JSON5 parser if you need them

How to Format JSON Online

Our JSON Formatter makes formatting effortless:

  1. Paste your minified or messy JSON into the input area
  2. Click Format to get properly indented output
  3. Choose your indent size: 2, 4, or 8 spaces
  4. The validator highlights any syntax errors with line and column information

For compressing JSON for transmission, click Minify to remove all whitespace and reduce file size.

Formatting JSON in Code

JavaScript / Node.js

const data = JSON.parse(minifiedJson);
const formatted = JSON.stringify(data, null, 2);

Python

import json
data = json.loads(minified_json)
formatted = json.dumps(data, indent=2)

Command Line

echo '{"key":"value"}' | python3 -m json.tool

JSON vs JSON5 vs JSONC

Standard JSON is strict — no comments, no trailing commas, no unquoted keys. Two common alternatives relax these rules:

  • JSON5 allows comments, trailing commas, unquoted keys, and single-quoted strings
  • JSONC (used in VS Code settings) allows comments but keeps other JSON strictness

For API communication, always use standard JSON. For configuration files where comments are helpful, consider JSON5 or YAML.

Best Practices

  • Always validate JSON before parsing in production code
  • Use a linter or formatter in your IDE to catch issues early
  • Prefer 2-space indentation for readability (it's the most common convention)
  • Minify JSON in production APIs to reduce bandwidth
  • Store formatted JSON in version control for readability

Try It Now

Use our free JSON Formatter to format, validate, and minify your JSON data — entirely in your browser, with zero data sent to any server.