← Back to Guides

JSON Pretty Print: How to Format JSON for Readability

· Tags: json, pretty-print, json-formatter, json-indentation, developer-tools

What is JSON Pretty Printing?

JSON pretty printing is the process of taking raw, compact (minified) JSON data and reformatting it with proper indentation, line breaks, and spacing to make it human-readable. A pretty-printed JSON document uses consistent indentation and clear structural formatting that reveals the hierarchy of nested objects and arrays.

Compare these two representations of the same data:

Minified (hard to read):

{"users":[{"id":1,"name":"Alice","roles":["admin","editor"]},{"id":2,"name":"Bob","roles":["viewer"]}],"total":2}

Pretty Printed (easy to read):

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "roles": ["admin", "editor"]
    },
    {
      "id": 2,
      "name": "Bob",
      "roles": ["viewer"]
    }
  ],
  "total": 2
}

Pretty Print vs Minified JSON

The choice between pretty-printed and minified JSON depends on the use case:

| Aspect | Pretty Print | Minified | |--------|-------------|----------| | Readability | High -- clear structure | Low -- all data on one line | | File size | Larger (more whitespace) | Smaller (no whitespace overhead) | | Network transfer | Slower for large payloads | Faster, less bandwidth | | Debugging | Ideal for development | Impractical for inspection | | Production use | Rarely used | Standard for API responses |

Production APIs typically serve minified JSON to reduce bandwidth, while developers pretty-print JSON during development and debugging.

Indentation Styles

Most formatters support two common indentation styles:

2-Space Indentation (Most Common)

{
  "name": "Alice",
  "age": 30,
  "address": {
    "city": "New York"
  }
}

Recommended for: JavaScript/TypeScript projects, modern web development, and most general use.

4-Space Indentation

{
    "name": "Alice",
    "age": 30,
    "address": {
        "city": "New York"
    }
}

Recommended for: Python projects (PEP 8 style) and teams that prefer wider indentation for clarity.

Tab Indentation

Some developers prefer tab-based indentation, particularly in accessibility-focused workflows where users adjust tab width in their editor.

Choosing the Right Indentation

Most teams standardize on 2 spaces because it balances readability with horizontal space efficiency. The JavaScript ecosystem (Node.js, React, Vue) overwhelmingly uses 2-space indentation. If you work in a Python-heavy environment, 4-space indentation may feel more natural. The key is consistency -- pick one style and enforce it across your project with a tool like Prettier or EditorConfig.

JSON Pretty Print in Different Programming Languages

JavaScript / Node.js

const formatted = JSON.stringify(data, null, 2);
console.log(formatted);

Python

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

Java

import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
String formatted = mapper.writerWithDefaultPrettyPrinter()
    .writeValueAsString(data);

Go

import "encoding/json"
formatted, _ := json.MarshalIndent(data, "", "  ")
fmt.Println(string(formatted))

Ruby

require 'json'
puts JSON.pretty_generate(data)

How to Pretty Print JSON

Using an Online JSON Formatter

The quickest method is to use a free online JSON formatter -- paste your JSON and get instant, formatted output with syntax highlighting and error detection.

Using Command Line Tools

With jq (most popular):

cat file.json | jq .

With Python:

python -m json.tool file.json

With Node.js:

node -e "const d = require('./file.json'); console.log(JSON.stringify(d, null, 2));"

Browser Developer Tools

All major browsers format JSON responses automatically in their Network tabs:

  • Chrome DevTools: Network tab automatically pretty-prints JSON responses
  • Firefox Developer Tools: JSON responses are displayed with collapsible tree views
  • Safari Web Inspector: JSON preview includes automatic formatting

IDE Plugins for JSON Formatting

VS Code

VS Code has excellent built-in JSON support. Use Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS) to format any JSON file. Additional extensions enhance the experience:

  • Prettier -- Formats JSON, JSONC, and many other languages consistently
  • JSON Tools -- Sort, minify, and format JSON with keyboard shortcuts
  • JavaScript Booster -- Advanced JSON manipulation features

JetBrains IDEs (IntelliJ, WebStorm, PyCharm)

  • Built-in formatter: Ctrl+Alt+L (Windows/Linux) or Cmd+Option+L (macOS)
  • Automatic JSON validation and formatting on paste

Vim / Neovim

" Format JSON in place
:%!jq .

Advanced JSON Formatting Options

Modern JSON formatters offer more than basic indentation:

  • Key sorting -- Alphabetically sort object keys for consistent output
  • Syntax highlighting -- Color-coded keys, strings, numbers, and booleans
  • Collapsible trees -- Expand and collapse nested structures for easier navigation
  • Line numbers -- Reference specific locations during debugging
  • Error highlighting -- Visually mark syntax errors with detailed messages

Best Practices for JSON Readability

  1. Always validate before formatting -- A formatter cannot fix invalid JSON. Use a JSON validator first to catch syntax errors.
  2. Set a team standard -- Agree on indentation (2 spaces is the industry standard) and enforce it with Prettier or a linter.
  3. Keep nesting shallow -- Deeply nested JSON (4+ levels) is difficult to read regardless of formatting. Consider restructuring when possible.
  4. Use meaningful keys -- Descriptive, concise key names improve readability more than any formatter.
  5. Add trailing newlines -- End JSON files with a newline character to comply with POSIX standards and avoid diff noise.

Try our free online JSON pretty print tool to instantly format and beautify your JSON data with customizable indentation and syntax highlighting.