pretty-print json in python (pythonic way)
json, python
Solution
Python's builtin JSON module can handle that for you:
>>> import json
>>> a = {'hello': 'world', 'a': [1, 2, 3, 4], 'foo': 'bar'}
>>> print(json.dumps(a, indent=2))
{
"hello": "world",
"a": [
1,
2,
3,
4
],
"foo": "bar"
}
Problem
I know that the `pprint` python standard library is for pretty-printing python data types. However, I'm always retrieving json data, and I'm wondering if there is any easy and fast way to pretty-print json data? No pretty-printing: ``` import requests r = requests.get('http://server.com/api/2/....') r.json() ``` With pretty-printing: ``` >>> import requests >>> from pprint import pprint >>> r = requests.get('http://server.com/api/2/....') >>> pprint(r.json()) ```