DEV Community

BHUVANESH M
BHUVANESH M

Posted on • Edited on

Python's Hidden Gem for JSON Indentation

Still pasting JSON into websites to read it? Python gives you a cleaner, offline way in one line.

πŸ“¦ The Built-in Way β€” No Extra Installs

Python ships with a powerful toolset for handling JSON.
Here's how to pretty-print JSON instantly:

import json with open("data.json") as f: data = json.load(f) print(json.dumps(data, indent=4)) 
Enter fullscreen mode Exit fullscreen mode

βœ… Indented
βœ… Readable
βœ… Offline & fast

πŸ”€ Bonus: From String Instead of File

json_str = '{"name":"name","skills":["skill1","skill2"]}' print(json.dumps(json.loads(json_str), indent=2)) 
Enter fullscreen mode Exit fullscreen mode

🧠 Real Uses

  • Debug API responses
  • View config files
  • Log structured data beautifully

🧼 Clean Trick: Sorting Keys Too

print(json.dumps(data, indent=4, sort_keys=True)) 
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Sometimes the most powerful tools are the ones built in.


πŸ“– For more tips and tricks in Python 🐍, check out

Packed with hidden gems, Python's underrated features and modules are real game-changers when it comes to writing clean and efficient code.


Top comments (0)