print pretty json python code example

Example 1: python print pretty json

import json

json_data = '[{"ID":10,"Name":"Pankaj","Role":"CEO"},' \
            '{"ID":20,"Name":"David Lee","Role":"Editor"}]'

json_object = json.loads(json_data)

json_formatted_str = json.dumps(json_object, indent=2)

print(json_formatted_str)

Example 2: pretty json python

print(json.dumps(dict, indent=4))

Example 3: python json.dumps pretty print

json.dumps(x, indent=4)

Example 4: response.json results in pretty data python

import requests
import json
r = requests.get('http://server.com/api/2/....')
pretty_json = json.loads(r.text)
print (json.dumps(pretty_json, indent=2))

Example 5: python format json file

python3 -m json.tool unformatted.json > formatted.json