How to prettyprint a JSON file?
To pretty-print a JSON file in Python, you can use the json
module. Here's an example:
import json
# Open the JSON file
with open('file.json', 'r') as f:
# Load the JSON file
data = json.load(f)
# Pretty-print the JSON file
print(json.dumps(data, indent=2))
This will read the JSON file and parse it into a Python object (a dictionary in this case). Then, it will print the dictionary in a pretty-printed format, with each level of the dictionary indented by 2 spaces.
You can also specify additional options when calling json.dumps()
, such as the sort order of the keys or the separators to use between keys and values. You can find more information about these options in the documentation for the json
module.