How to Format JSON: A Complete Guide

JSON (JavaScript Object Notation) is the most widely used data interchange format. But raw JSON is often minified, unindented, or difficult to read. This guide explains why formatting matters and how to do it effectively.

Why Format JSON?

Unformatted JSON is functionally correct but painful to work with. Consider this API response:

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

Now compare it with formatted JSON:

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

The second version reveals the structure immediately: an object containing a users array, each with an id, name, email, and roles array.

When to Format JSON

Indentation Styles

2 Spaces (Recommended)

The most popular choice. Used by Prettier, ESLint, and many major projects. Balances readability with compactness.

{
  "name": "JSONCraft",
  "version": "1.0"
}

4 Spaces

Provides more visual separation between levels. Common in languages with deeper nesting.

{
    "name": "JSONCraft",
    "version": "1.0"
}

Tab Indentation

Uses tab characters instead of spaces. Less common for JSON but useful when your team or project requires it.

How to Format JSON Online

  1. Open the JSON Formatter.
  2. Paste your unformatted JSON into the input editor.
  3. Select your preferred indentation (2 spaces is the default).
  4. Click Format or press Ctrl+Enter.
  5. Copy or download the formatted result.

Formatting JSON in Code

Most programming languages have built-in JSON formatting:

JavaScript

JSON.stringify(data, null, 2);

Python

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

Bash (using jq)

cat data.json | jq '.'

Common Mistakes

Best Practices

🔒
Try the JSON Formatter to format your JSON instantly. All processing happens in your browser.