How to Format JSON: A Complete Guide to Prettifying JSON

July 31, 20267 min read

Unformatted JSON is nearly unreadable. A minified API response can stretch across hundreds of characters on a single line. Proper formatting — also called "pretty-printing" or "beautifying" — breaks it into readable, indented structure in seconds.

This guide covers JSON formatting rules, how to do it in code and in the browser, common errors you will encounter, and the difference between formatting and minifying.

What is JSON Formatting?

JSON formatting adds consistent whitespace — line breaks and indentation — to a JSON document to make it human-readable. The structure and data are unchanged; only the presentation differs.

Minified (before):

{"name":"Alice","age":30,"city":"Auckland","skills":["TypeScript","Python","Go"]}

Formatted (after, 2-space indentation):

{
  "name": "Alice",
  "age": 30,
  "city": "Auckland",
  "skills": [
    "TypeScript",
    "Python",
    "Go"
  ]
}

Why Formatting Matters

JSON Formatting Rules

The spec (RFC 8259) has a few strict rules that govern valid JSON:

How to Format JSON in JavaScript

JSON.stringify() accepts a third argument for indentation:

const data = { name: 'Alice', roles: ['admin', 'editor'], active: true }

// 2-space indentation (most common in JS/TS)
console.log(JSON.stringify(data, null, 2))

// 4-space indentation
console.log(JSON.stringify(data, null, 4))

// Tab indentation
console.log(JSON.stringify(data, null, '	'))

// Filter keys using the replacer argument
const filtered = JSON.stringify(user, ['name', 'email'], 2)

How to Format JSON in Python

import json

data = {'name': 'Alice', 'roles': ['admin', 'editor'], 'active': True}

# 2-space indentation
print(json.dumps(data, indent=2))

# 4-space indentation (Python convention)
print(json.dumps(data, indent=4))

# Sort keys and preserve non-ASCII characters
print(json.dumps(data, indent=2, sort_keys=True, ensure_ascii=False))

# Format a file from the command line:
# python3 -m json.tool input.json

From the shell using jq:

# Format a file
jq '.' data.json

# Format an API response
curl -s https://api.example.com/users | jq '.'

Format JSON Online

IRJSON is the fastest way to format JSON in the browser. Paste your JSON, press Ctrl+Enter (or Cmd+Enter on Mac), and it is instantly formatted with syntax highlighting:

Common JSON Formatting Errors and Fixes

Trailing commas

// Invalid — trailing comma after last item
{
  "name": "Alice",
  "roles": ["admin", "editor"],
}

// Fixed
{
  "name": "Alice",
  "roles": ["admin", "editor"]
}

Single-quoted strings

// Invalid — JSON requires double quotes
{ 'name': 'Alice' }

// Fixed
{ "name": "Alice" }

Comments

// Invalid — JSON does not support comments
{
  // This is the user object
  "name": "Alice"
}

Remove comments, or switch to YAML if you need inline documentation. IRJSON's JSON Repair tool automatically strips comments and fixes other common errors.

undefined and NaN

// Invalid — JavaScript-only values
{ "value": undefined, "count": NaN }

// Fixed
{ "value": null, "count": null }

Indentation Styles: 2 Spaces vs 4 Spaces vs Tabs

The JSON spec treats all whitespace as equivalent — there is no mandated indentation. Convention by ecosystem:

The most important thing is consistency within a project. Configure your editor and formatter to enforce the chosen style automatically.

Minifying vs Formatting

Minification removes all unnecessary whitespace to produce the most compact valid JSON. Useful for reducing API response payload size, shrinking embedded JSON in JavaScript bundles, and reducing storage costs.

// Formatted (54 bytes)
{
  "name": "Alice",
  "age": 30
}

// Minified (27 bytes)
{"name":"Alice","age":30}

Frequently Asked Questions

What is the difference between JSON and JSONC?

JSONC (JSON with Comments) allows // and /* */ comments. It is not valid standard JSON — JSON.parse will reject it. VS Code uses JSONC for its configuration files and parses them with a JSONC-aware parser.

What is JSON5?

JSON5 is a superset of JSON designed to be more human-friendly. It adds comments, trailing commas, single-quoted strings, unquoted keys, multi-line strings, and Infinity/NaN. JSON5 requires its own parser (the json5 npm package) and is not supported natively in browsers or Node.js.

What is the standard JSON indentation?

There is no single standard — all whitespace is equivalent to JSON parsers. 2 spaces is the most common choice in JavaScript/TypeScript projects; 4 spaces is common in Python and Java. Pick one and enforce it in your team's editor and CI pipeline.

Format JSON instantly, free

IRJSON formats, validates and highlights JSON in your browser — no sign-up, no ads, 100% private.

Open Formatter →