jsondecode.com logo

HomeChevronBlogChevronPretty Print JSON: All Methods Explained

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.

author

Shashank Jain

Author

14/06/20261 minute 3 seconds read
Pretty Print JSON: All Methods ExplainedPretty Print JSON: All Methods Explained

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

View all

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.