X Xerobit

JSON Beautifier — Format and Prettify JSON Instantly

A JSON beautifier adds proper indentation and line breaks to minified JSON, making it human-readable. Here's how JSON formatting works, common formatting options, and why...

Mian Ali Khalid · · 5 min read
Use the tool
JSON Formatter
Format, validate, and beautify JSON online. 100% client-side — your data never leaves your browser.
Open JSON Formatter →

A JSON beautifier takes compact, minified JSON and expands it with consistent indentation and line breaks. The data is identical — the structure and values don’t change — but it becomes readable to humans.

Minified: {"name":"Alice","age":30,"active":true}

Beautified:

{
  "name": "Alice",
  "age": 30,
  "active": true
}

Use the JSON Formatter to beautify, validate, and minify JSON in your browser.

Why JSON gets minified in the first place

JSON in production API responses is typically minified to reduce payload size. Whitespace (spaces, newlines, indentation) adds bytes without adding information. For large API responses, this matters:

// Minified (86 bytes):
{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}],"total":2}

// Beautified (120 bytes — 40% larger):
{
  "users": [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Bob"}
  ],
  "total": 2
}

At scale — millions of API calls per day, large response payloads — minification meaningfully reduces bandwidth. For a 10KB response body, minification saves ~2KB per request. At 10 million requests/day, that’s 20GB of bandwidth saved.

You shouldn’t need to read minified JSON — it’s for machines. When you need to read it, a beautifier is the correct tool.

Indentation options

JSON has no required indentation style. Common conventions:

2-space indentation (most common in JavaScript/Node.js):

{
  "key": "value",
  "nested": {
    "key": "value"
  }
}

4-space indentation (Python, Java):

{
    "key": "value",
    "nested": {
        "key": "value"
    }
}

Tab indentation:

{
	"key": "value",
	"nested": {
		"key": "value"
	}
}

The JSON Formatter defaults to 2-space indentation. The choice doesn’t matter as long as it’s consistent — JSON is valid with any whitespace.

JSON beautifiers validate while formatting

A good beautifier also validates the JSON. Minified JSON is easy to have errors in — a missing comma or unquoted key is invisible until parsing fails.

Common errors beautifiers catch:

  • Missing comma between properties or array elements
  • Trailing comma after the last element (invalid in JSON, valid in JavaScript)
  • Unquoted string keys ({name: "Alice"} is not valid JSON)
  • Single-quoted strings ({'name': 'Alice'} is not valid JSON)
  • Comments (// this is not JSON, /* nor this */)
  • Undefined, NaN, or Infinity values (not valid JSON)

These are all valid in JavaScript objects but invalid in JSON. If you’re debugging why JSON.parse() is throwing an error, a beautifier will highlight exactly where the problem is.

How to beautify JSON in code

JavaScript

// Parse and re-stringify with indentation:
const minified = '{"name":"Alice","age":30}';
const parsed = JSON.parse(minified);
const beautified = JSON.stringify(parsed, null, 2);
console.log(beautified);
// {
//   "name": "Alice",
//   "age": 30
// }

// Third argument to JSON.stringify = indent size (2 = 2 spaces)
// Use '\t' for tab indentation:
const tabbed = JSON.stringify(parsed, null, '\t');

Python

import json

minified = '{"name":"Alice","age":30}'
parsed = json.loads(minified)
beautified = json.dumps(parsed, indent=2)
print(beautified)
# {
#   "name": "Alice",
#   "age": 30
# }

# Sort keys alphabetically:
sorted_json = json.dumps(parsed, indent=2, sort_keys=True)

# From a file:
with open('data.json') as f:
    data = json.load(f)
print(json.dumps(data, indent=2))

Command line (jq)

# Beautify from pipe:
echo '{"name":"Alice","age":30}' | jq .

# Beautify a file:
jq . input.json

# Minify a file:
jq -c . input.json

# Extract a specific value while formatting:
curl https://api.example.com/data | jq '.users[0].name'

jq is the standard CLI JSON processor. It’s available via package managers (apt install jq, brew install jq).

Command line (Python one-liner)

# Beautify from pipe:
echo '{"name":"Alice","age":30}' | python3 -m json.tool

# Beautify a file:
python3 -m json.tool input.json

# With 4-space indent:
python3 -m json.tool --indent 4 input.json

No dependencies needed — Python’s json.tool module is part of the standard library.

Beautifying JSON vs sorting JSON

Beautification only changes whitespace. Sorting rearranges keys alphabetically.

Sorted JSON is useful for:

  • Diffing two JSON objects (without sorting, key order differences show as changes)
  • Canonical representations for hashing
  • Readability when you know the key names
// Unsorted:
{
  "name": "Alice",
  "age": 30,
  "active": true
}

// Sorted:
{
  "active": true,
  "age": 30,
  "name": "Alice"
}

Note: JSON spec says object key order is undefined. Parsers and serializers may not preserve the order. Sorting is a display choice.

Large JSON files

For very large JSON (megabytes), a browser-based beautifier may hit memory limits or become slow. Options for large files:

jq (CLI): Handles gigabyte-scale JSON files via streaming. Much faster than browser tools.

VS Code: Built-in JSON formatting (Shift+Alt+F on Windows) handles large files.

Python: json.tool works on large files but loads entirely into memory.

The JSON Formatter works well for typical API responses and config files (up to ~1MB in most browsers).


Related posts

Related tool

JSON Formatter

Format, validate, and beautify JSON online. 100% client-side — your data never leaves your browser.

Written by Mian Ali Khalid. Part of the Data & Format pillar.