- Регистрация
- 1 Мар 2015
- Сообщения
- 1,481
- Баллы
- 155
? The Built-in Way — No Extra InstallsStill pasting JSON into websites to read it? Python gives you a cleaner, offline way in one line.
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))
? Bonus: From String Instead of File
json_str = '{"name":"name","skills":["skill1","skill2"]}'
print(json.dumps(json.loads(json_str), indent=2))
? Real Uses
- Debug API responses
- View config files
- Log structured data beautifully
print(json.dumps(data, indent=4, sort_keys=True))
? Sometimes the most powerful tools are the ones built in.