Blog post
Pretty Print JSON: All Methods Explained
Pretty print JSON in JavaScript, Python, terminal, and online. Format minified JSON for readability with code examples.
Shashank Jain
Author


Article
What Is Pretty Printing?
Pretty printing adds indentation and line breaks to minified JSON, making it human-readable.
JavaScript: JSON.stringify
const obj = { name: 'Alice', address: { city: 'NYC' } };
// 2-space indent (most common)
console.log(JSON.stringify(obj, null, 2));
// Tab indent
console.log(JSON.stringify(obj, null, '\t'));JavaScript: Selective Fields with Replacer
const user = { id: 1, name: 'Alice', password: 'secret', email: 'a@b.com' };
console.log(JSON.stringify(user, ['id', 'name', 'email'], 2));Python: json.dumps
import json
data = {"name": "Alice", "scores": [95, 87, 91]}
pretty = json.dumps(data, indent=2, sort_keys=True)
print(pretty)Terminal / Command Line
# Using Python
echo '{"name":"Alice"}' | python3 -m json.tool
# Using jq
cat data.json | jq .
# jq with filter
curl -s https://api.example.com/user | jq '.name, .email'Sorting Keys
# Python
json.dumps(data, indent=2, sort_keys=True)
# jq
cat data.json | jq -S .FAQ
What is the best indent size for JSON?
2 spaces is most common in JavaScript/web projects. 4 spaces is common in Python. For machine-to-machine JSON, use no indentation (minified) to save bandwidth.
How do I pretty print JSON in VS Code?
Open a .json file, press Shift+Alt+F (Windows/Linux) or Shift+Option+F (Mac). Or use the jsondecode.com online formatter.
Does pretty printing change the data?
No. Pretty printing only adds whitespace. The data is semantically identical — JSON ignores whitespace between tokens.
How do I pretty print JSON from an API response?
In JavaScript: const text = await res.text(); console.log(JSON.stringify(JSON.parse(text), null, 2));
What does jq do that JSON.stringify cannot?
jq is a full query language for JSON — it can filter, transform, map, and restructure. JSON.stringify only serializes.
Keep reading
Recent blogs

Jun 14, 2026
JSON in C#: System.Text.Json and Newtonsoft Complete Guide
Serialize and deserialize JSON in C# using System.Text.Json and Newtonsoft.Json with practical examples.

Jun 14, 2026
JSON to Markdown Table: Convert JSON Arrays Instantly
Convert JSON arrays to Markdown tables in JavaScript, Python, and with the free online tool.

Jun 14, 2026
JSON in TypeScript: Type-Safe Parsing and Validation
Stop using any for JSON in TypeScript — use Zod, type guards, and generics for fully type-safe parsing.