In This Guide
- 1What JSON formatting actually does
- 2When to format vs when to minify
- 3Method 1 — Format JSON online (no code, instant)
- 4Method 2 — Format JSON in Python
- 5Method 3 — Format JSON in JavaScript / Node.js
- 6Method 4 — Format JSON in VS Code
- 7Method 5 — Format JSON with curl on the command line
- 8Common JSON validation errors and fixes
- 9Frequently asked questions
What JSON Formatting Actually Does
JSON formatting — also called JSON beautification or pretty-printing — adds indentation, line breaks, and consistent spacing to a JSON document. It does not change the data; it only changes how the data is visually presented.
The same object can be represented as a single line of 200 characters or as 20 clearly indented lines. Both represent identical data and will parse to the same result. The formatted version is for humans; the minified version is for machines.
{
product: {
id: "SKU-001",
name: "Laptop",
price: 999.99,
inStock: true,
tags: ["electronics", "computing"],
},
}{
product: {
id: "SKU-001",
name: "Laptop",
price: 999.99,
inStock: true,
tags: ["electronics", "computing"],
},
}When to Format vs When to Minify
Format JSON when:
- Debugging an API response you did not write
- Reviewing a pull request containing config changes
- Writing documentation or code examples
- Inspecting database documents or query results
- Onboarding new team members to a data schema
Minify JSON when:
- Sending API responses in production to reduce bandwidth
- Storing JSON in databases where size affects cost
- Embedding JSON in HTML data attributes
- Optimising payload sizes for mobile clients
- Writing config files for deployment pipelines
Method 1 — Format JSON Online (No Code Required)
For one-off formatting, debugging API responses, or when you are on a machine without a development environment, a browser tool is the fastest option. No installation, no terminal, no dependencies.
QuickTextTools — JSON Formatter
Paste your JSON, choose 2-space or 4-space indentation, and click Format. Validation happens automatically — errors are highlighted with explanatory messages. Everything runs in your browser, so your data never reaches any server.
Open JSON Formatter ToolCopy the minified or messy JSON from your API response, log file, or source
Go to the JSON Formatter tool linked above
Paste the JSON into the input field
Select your preferred indentation (2 or 4 spaces)
Click Format JSON — validation and formatting happen simultaneously
Copy the output or download it as a .json file
Method 2 — Format JSON in Python
Python's built-in{" "} <code className="bg-gray-100 px-2 py-0.5 rounded font-mono"> json </code>{" "} module handles formatting with zero third-party dependencies.
import json
Minified JSON string (e.g. from an API response)
raw = '{"user":{"id":42,"name":"Alice","roles":["admin","editor"]}}'
Parse and re-serialize with indentation
formatted = json.dumps(json.loads(raw), indent=2)
print(formatted)} </pre> </div> <div className="bg-gray-900 rounded-xl p-6 my-6"> <div className="text-gray-400 text-xs font-mono mb-3">python — format a JSON file</div> <pre className="text-green-400 font-mono text-sm overflow-x-auto"> {import json
Read a minified JSON file and write formatted output
with open("data.json", "r") as f:
data = json.load(f)
with open("data_formatted.json", "w") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
print("Formatted file saved.")Pro tip: Pass ensure_ascii=False to preserve Unicode characters (emoji, accented letters, CJK) instead of escaping them to \uXXXX sequences. Pass sort_keys=True to alphabetically sort all keys — useful for diffs and audits.
Method 3 — Format JSON in JavaScript / Node.js
JavaScript's{" "} <code className="bg-gray-100 px-2 py-0.5 rounded font-mono"> JSON.stringify() </code>{" "} accepts an indentation argument as its third parameter.
// Format a JSON string
const raw = '{"user":{"id":42,"name":"Alice","roles":["admin","editor"]}}';
const formatted = JSON.stringify(JSON.parse(raw), null, 2);
console.log(formatted);
// Format a JavaScript object
const obj = { name: "Alice", age: 30, active: true };
console.log(JSON.stringify(obj, null, 2));
// Minify a JSON string (remove all whitespace)
const minified = JSON.stringify(JSON.parse(raw));
console.log(minified);} </pre> </div> <div className="bg-gray-900 rounded-xl p-6 my-6"> <div className="text-gray-400 text-xs font-mono mb-3">node.js — read file, format, and write</div> <pre className="text-green-400 font-mono text-sm overflow-x-auto"> {const fs = require("fs");
const raw = fs.readFileSync("data.json", "utf8");
const formatted = JSON.stringify(JSON.parse(raw), null, 2);
fs.writeFileSync("data_formatted.json", formatted);
console.log("Formatted and saved.");<strong>Note:</strong> The second argument to JSON.stringify() is a <em>replacer</em> — pass{" "} <code className="bg-amber-100 px-1 rounded">null</code> to include all keys. You can pass an array of key names to filter output, or a function for custom serialization. The third argument sets indentation: pass a number (spaces) or a string (e.g. <code className="bg-amber-100 px-1 rounded">" "</code>{" "} for tabs).
Method 4 — Format JSON in VS Code
VS Code has built-in JSON formatting that respects your .editorconfig and Prettier settings.
Method 5 — Format JSON on the Command Line with jq
jq is the standard command-line JSON processor, available on Linux, macOS (via Homebrew), and Windows (via Chocolatey or WSL).
# Format a JSON file jq . data.json Format and save to a new file jq . data.json > data_formatted.json Format an API response directly curl -s https://api.example.com/users | jq . Format and filter — show only the name field curl -s https://api.example.com/users | jq '.[].name' Minify a formatted file jq -c . data_formatted.json > data_minified.json
For simple pretty-printing without jq, Python ships with a JSON tool module: cat data.json | python3 -m json.tool. This works on any machine with Python 3 and requires no additional installation.
Common JSON Validation Errors and How to Fix Them
Unexpected token , in JSON at position N
Cause: Trailing comma after the last property or array element. Valid JavaScript, invalid JSON.
Fix: Find patterns like ,} or ,] and remove the comma.
Unexpected token ' in JSON
Cause: Single-quoted strings. JSON requires double quotes for all strings — keys and values.
Fix: Replace all single quotes with double quotes, taking care around apostrophes in values.
Unexpected token // or /* in JSON
Cause: Comments. JSON does not support comments of any kind.
Fix: Remove all // and /* ... */ comments. Use JSON5 or JSONC if you need annotated config files.
Unexpected end of JSON input
Cause: The JSON string is truncated — the last bracket or brace is missing.
Fix: Verify the complete JSON was copied. Count opening vs closing brackets — they must match.
Related Tools & Resources
Frequently Asked Questions
Ready to Format Your JSON?
Skip the setup for quick formatting jobs. Paste your JSON, click Format, and get clean readable output in under a second — no account needed.
Open JSON Formatter Tool