make json look pretty python code example
Example 1: print json python
import json
uglyjson = '{"firstnam":"James","surname":"Bond","mobile":["007-700-007","001-007-007-0007"]}'
#json.load method converts JSON string to Python Object
parsed = json.loads(uglyjson)
print(json.dumps(parsed, indent=2, sort_keys=True))
Example 2: python write to json with indent
import json
# alphabetic dictionary
json_data = {hex(k): chr(k) for k in range(ord('a'), ord('z') + 1)}
# to file
with open("my_json.json", "w") as file:
json.dump(json_data, file, indent=4, sort_keys=True)