How to Format JSON
Raw JSON from an API is usually minified — one long line that's impossible to read. Here is how to format it into readable indented JSON, online or in code.
Method 1: Online (fastest)
Go to quicktoolshub.org/json-formatter. Paste your JSON, choose 2 or 4 space indentation, and click Format JSON. The output is syntax-highlighted and ready to copy. Nothing is sent to a server.
Method 2: JavaScript
// Format an object const formatted = JSON.stringify(obj, null, 2); // Format a JSON string (parse first, then re-stringify) const formatted = JSON.stringify(JSON.parse(rawString), null, 2); // 4-space indent const formatted = JSON.stringify(obj, null, 4); // With sorted keys const formatted = JSON.stringify(obj, Object.keys(obj).sort(), 2);
Method 3: Python
import json
# Format a dict
print(json.dumps(data, indent=2))
# Format a JSON string
print(json.dumps(json.loads(raw_string), indent=2))
# With sorted keys
print(json.dumps(data, indent=2, sort_keys=True))
# Format a JSON file
with open('input.json') as f:
data = json.load(f)
with open('output.json', 'w') as f:
json.dump(data, f, indent=2)Method 4: Command line
# jq — most popular, colorized output
cat file.json | jq .
echo '{"key":"value"}' | jq .
# Python built-in (any system with Python 3)
python3 -m json.tool file.json
cat file.json | python3 -m json.tool
# Node.js (inline)
node -e "console.log(JSON.stringify(require('./file.json'), null, 2))"Method 5: VS Code
Open the JSON file. Use Shift+Alt+F (Windows/Linux) or Shift+Option+F (Mac) to format the document. Right-click > Format Document also works. VS Code uses your editor settings for indentation (tab size).
Why JSON formatting matters
Raw JSON from an API or log file is typically minified — all whitespace removed to reduce transmission size. While compact JSON is efficient to send, it is nearly impossible to read or debug. A single minified API response can be thousands of characters on one line. Formatting it with indentation immediately reveals the structure: which keys are nested, where arrays start and end, and which values are strings vs numbers vs booleans.
Validation is equally important. JSON has strict syntax rules: property names must be in double quotes (not single), trailing commas are not allowed, and there is no comment syntax. A single missing comma or extra bracket will cause a parse error in any JSON consumer. Formatting tools that validate as they format catch these errors before they cause silent failures in production code.
2-space indentation is the most common standard, used by npm, Prettier, and most JavaScript style guides. 4-space indentation is preferred in some Python projects and older codebases. Tab indentation is used in a few projects for accessibility reasons (allows users to control indent width). The choice does not affect how the JSON parses — it is purely cosmetic.
Method 6: Node.js / npm
# Format JSON in a script
const fs = require('fs');
const data = JSON.parse(fs.readFileSync('input.json', 'utf8'));
fs.writeFileSync('output.json', JSON.stringify(data, null, 2));
# Using prettier CLI to format JSON files
npx prettier --write "**/*.json"Format JSON now — free, instant
Paste raw JSON and get it formatted with syntax highlighting. No sign-up, no server.
Open JSON Formatter →Frequently asked questions
How do I format JSON online?
Go to quicktoolshub.org/json-formatter. Paste your raw JSON, click Format JSON, and get the indented, syntax-highlighted output instantly. The tool runs in your browser — no data is sent to a server.
How do I format JSON in JavaScript?
Use JSON.stringify with the indent parameter: JSON.stringify(obj, null, 2) formats with 2-space indentation. To parse and re-format a JSON string: JSON.stringify(JSON.parse(jsonString), null, 2). Pass null as the second argument (replacer) and 2 or 4 as the third (indent spaces).
How do I format JSON in Python?
Use the json module: import json; formatted = json.dumps(data, indent=2). To format a JSON string: formatted = json.dumps(json.loads(json_string), indent=2). For sorting keys alphabetically: json.dumps(data, indent=2, sort_keys=True). To print it: print(json.dumps(data, indent=2)).
How do I format JSON in VS Code?
Open the JSON file in VS Code. Right-click in the editor and select Format Document, or use the keyboard shortcut Shift+Alt+F (Windows/Linux) or Shift+Option+F (Mac). VS Code formats the JSON with 2 or 4 space indentation depending on your editor settings. Make sure the language is set to JSON in the bottom status bar.
How do I format JSON in the command line?
Using jq (the most common tool): echo '{"key":"value"}' | jq . or cat file.json | jq . The jq tool is available on most systems and formats JSON with color. Using Python (built-in): echo '{"key":"value"}' | python3 -m json.tool. Using Node.js: node -e "console.log(JSON.stringify(require('./file.json'), null, 2))".
What is the difference between JSON format and JSON minify?
JSON format (also called pretty-print or beautify) adds indentation and newlines to make the JSON human-readable. JSON minify removes all unnecessary whitespace, producing the smallest possible JSON string. Formatted JSON is better for reading and debugging. Minified JSON is better for network transmission and storage, as it reduces file size significantly.