JSON Formatter

Beautify and format JSON with customizable indentation and sorting options. Our formatter helps you make JSON readable and properly structured for development and debugging.

Features:

  • • Format with 2, 4, or 8 space indentation
  • • Minify JSON to reduce file size
  • • Sort object keys alphabetically
  • • Real-time size comparison
  • • Copy formatted output to clipboard

Formatting Options

JSON Input

Paste your JSON data here

Formatted Output

🎨 Formatting Options

Indentation: Choose between 2, 4, or 8 spaces for better readability.
Sort Keys: Alphabetically order object keys for consistency.
Minify: Remove all whitespace to reduce file size.
Size Analysis: See how formatting affects your JSON size.

⚡ Quick Actions

Try These Examples

Click any example to load it into the formatter

API Response

Typical REST API response

{"users":[{"id":1,"name":"Alice Johnson","email":"alice@example.com","role":"adm...

Configuration File

Application configuration

{"app":{"name":"MyApp","version":"1.0.0","debug":true},"database":{"host":"local...

E-commerce Data

Product catalog entry

{"product":{"id":"prod-123","title":"Wireless Headphones","price":99.99,"currenc...

📚 JSON Formatting Guide

Why Format JSON?

Raw JSON data — especially from APIs, databases, or minified files — is often delivered as a single line of text with no whitespace. While this is efficient for machines, it is nearly impossible for humans to read and debug. JSON formatting (also called "pretty printing" or "beautifying") adds indentation, line breaks, and consistent spacing to make the structure visible at a glance.

Well-formatted JSON is essential for:

  • Debugging: Quickly spot missing values, wrong types, or structural issues in API responses and data files.
  • Code reviews: Reviewers can understand JSON config changes in pull requests without mentally parsing dense one-liners.
  • Documentation: Formatted JSON examples in docs and READMEs are far more readable than minified blobs.
  • Version control: Consistently formatted JSON produces cleaner diffs in Git, making changes easier to track.

Formatting vs. Minification

These are opposite operations, each suited to different stages of development:

Formatting (Pretty Print)

Adds indentation and line breaks for human readability. A minified API response like:

{"users":[{"id":1,"name":"Alice","email":"alice@example.com"},{"id":2,"name":"Bob","email":"bob@example.com"}]}

Becomes:

{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "email": "alice@example.com"
    },
    {
      "id": 2,
      "name": "Bob",
      "email": "bob@example.com"
    }
  ]
}

Minification

Removes all unnecessary whitespace to reduce file size. Use minification when:

  • Sending API responses — smaller payloads mean faster network transfer and lower bandwidth costs.
  • Storing in databases — whitespace adds no value in stored data and wastes storage space.
  • Embedding in HTML/JS — minified JSON in script tags reduces page load time.
  • Message queues — smaller messages improve throughput in Kafka, RabbitMQ, and similar systems.

A typical JSON document shrinks by 30-60% when minified, depending on its nesting depth and formatting.

Indentation: 2 Spaces vs. 4 Spaces vs. Tabs

The indentation style you choose affects readability, file size, and team consistency:

  • 2 spaces — the most common choice in the JavaScript and web development ecosystem. Compact enough for deeply nested structures while still readable. Used by default in JSON.stringify(data, null, 2).
  • 4 spaces — preferred in Python, Java, and enterprise environments. Provides clearer visual hierarchy for wide monitors but can push deeply nested code off-screen.
  • Tabs — rarely used in JSON. While tabs allow each developer to set their preferred visual width, most JSON tools and linters default to spaces.

Our recommendation: use 2 spaces for JSON files and match whatever your team or project has standardized on.

Sorting Keys Alphabetically

Sorting object keys produces consistent output regardless of insertion order. This is valuable for:

  • Version control diffs — when keys are sorted, changes show up as clean, minimal diffs instead of noisy reorderings.
  • Comparing documents — sorted keys make manual and automated JSON comparison much easier.
  • API documentation — alphabetical ordering helps readers find specific fields quickly.
  • Deterministic output — sorted keys ensure the same data always produces the same JSON string, which is important for caching and checksums.

Note: the JSON specification says object key order is not significant, so sorting does not change the meaning of your data.

How to Format JSON in Your Code

JavaScript / Node.js

// Pretty print with 2-space indent
const formatted = JSON.stringify(data, null, 2);

// Minify
const minified = JSON.stringify(data);

// Sort keys and format
const sorted = JSON.stringify(data, Object.keys(data).sort(), 2);

Python

import json

# Pretty print with 2-space indent
formatted = json.dumps(data, indent=2)

# Sort keys and format
sorted_json = json.dumps(data, indent=2, sort_keys=True)

# Minify (remove whitespace)
minified = json.dumps(data, separators=(',', ':'))

Command Line (jq)

# Format a JSON file
cat data.json | jq '.'

# Minify a JSON file
cat data.json | jq -c '.'

# Sort keys
cat data.json | jq -S '.'

JSON Formatting Best Practices

  • Be consistent — pick one indentation style (2 or 4 spaces) and use it everywhere in your project. Add a .editorconfig or .prettierrc to enforce it.
  • Format in development, minify in production — keep source files readable and let your build process handle minification.
  • Sort keys in config files — configuration files checked into Git benefit from sorted keys for cleaner diffs.
  • Validate before formatting — formatting invalid JSON will fail silently or produce unexpected results. Always validate first.
  • Watch file sizes — a 1 MB minified JSON file can become 3-4 MB when formatted. Be mindful of this when working with large datasets.
  • Use automation — configure your editor or CI pipeline to auto-format JSON files on save or commit, eliminating manual formatting work.
  • Preserve key order when it matters — some applications depend on key order (e.g., package.json conventions). Only sort keys when order is truly insignificant.

Common Formatting Mistakes

  • Mixed indentation — mixing tabs and spaces in the same file causes alignment issues and confusing diffs.
  • Inconsistent nesting — some objects indented with 2 spaces and others with 4 makes the structure hard to follow.
  • Formatting binary or encoded data — Base64 strings and binary blobs should stay on a single line. Formatting them adds line breaks that can corrupt the data.
  • Over-formatting — very short objects like {"x": 1, "y": 2} are sometimes more readable on a single line. Not everything needs to be expanded.

We use cookies to enhance your experience. By continuing to visit this site you agree to our use of cookies. Learn more