How to Format JSON: A Complete Guide for Developers

Raw JSON is hard to read. This guide shows you how to format JSON for readability — using code, command-line tools, and our free online formatter.

What Is JSON Formatting?

JSON formatting is the process of adding whitespace — indentation, line breaks, and spacing — to JSON data so humans can read it. The underlying data does not change. Only the presentation changes.

Unformatted
{"users":[{"id":1,"name":"Alice","roles":["admin"]},{"id":2,"name":"Bob","roles":["viewer"]}],"settings":{"theme":"dark","notifications":true}}
Formatted
{
  "users": [
    {
      "id": 1,
      "name": "Alice",
      "roles": ["admin"]
    },
    {
      "id": 2,
      "name": "Bob",
      "roles": ["viewer"]
    }
  ],
  "settings": {
    "theme": "dark",
    "notifications": true
  }
}

Why Formatting JSON Matters

Readability

Nested objects and arrays are impossible to scan in a single line. Proper indentation reveals the structure instantly.

Debugging

When an API returns malformed JSON, formatting it is the first step to finding the error.

Version Control

Formatted JSON produces meaningful diffs. Minified JSON shows the entire line changed for a single character edit.

How to Format JSON Manually

JavaScript: JSON.stringify()

const obj = { name: "Alice", age: 30 };

// Format with 2-space indentation
const formatted = JSON.stringify(obj, null, 2);
console.log(formatted);

// Output:
// {
//   "name": "Alice",
//   "age": 30
// }

Python: json.dumps()

import json

data = {"name": "Alice", "age": 30}

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

Command Line: jq

# Format a JSON file
jq . data.json > formatted.json

# Format from stdin
cat data.json | jq .

# Format with 4-space indentation
jq --indent 4 . data.json

Common JSON Formatting Mistakes

Trailing commas after the last property

JSON does not allow trailing commas. Remove the comma after the last key-value pair.

Mixing single and double quotes

JSON requires double quotes for strings and keys. Single quotes are invalid.

Leaving keys unquoted

All object keys must be wrapped in double quotes. Unquoted keys are JavaScript-only.

Adding comments

Standard JSON does not support comments. Remove all // and /* */ comments.

How to Format JSON Online

If you do not want to write code or install tools, use our free JSON formatter. It runs entirely in your browser — no data is uploaded.

1

Copy your raw JSON from an API response, log file, or database export.

2

Go to JSON Formatter Online and paste it into the input box.

3

Click the Format button.

4

Copy the formatted result or download it as a .json file.

Frequently Asked Questions

What is the standard indentation for JSON?

Two spaces is the most common convention, but four spaces and tabs are also valid. The key is consistency within your project.

Does formatting JSON change its data?

No. Formatting only changes whitespace — indentation, line breaks, and spacing. The actual data structure and values remain identical.

Can I format JSON without an internet connection?

Yes. Use built-in tools like JSON.stringify() in JavaScript, json.dumps() in Python, or jq on the command line.

Why does my formatted JSON look different in different tools?

Different formatters use different indentation defaults (2 vs 4 spaces), key sorting rules, and line wrapping. Most tools let you configure these.

Should I commit formatted or minified JSON to git?

Commit formatted JSON for readability during code review. Use minified JSON only for production builds or API responses where size matters.

Format Your JSON Now

Paste your raw JSON into our free formatter. Get perfectly indented output in one click.

Try JSON Formatter Online →