You should use the optional argument indent.
header, output = client.request(twitterRequest, method="GET", body=None,
headers=None, force_auth_header=True)
# now write output to a file
with open("twitterData.json", "w") as twitterDataFile:
# magic happens here to make it pretty-printed
twitterDataFile.write(
simplejson.dumps(simplejson.loads(output), indent=4, sort_keys=True)
)
Answer from mattbornski on Stack Overflow Top answer 1 of 8
183
You should use the optional argument indent.
header, output = client.request(twitterRequest, method="GET", body=None,
headers=None, force_auth_header=True)
# now write output to a file
with open("twitterData.json", "w") as twitterDataFile:
# magic happens here to make it pretty-printed
twitterDataFile.write(
simplejson.dumps(simplejson.loads(output), indent=4, sort_keys=True)
)
2 of 8
147
You can parse the JSON, then output it again with indents like this:
import json
mydata = json.loads(output)
print json.dumps(mydata, indent=4)
See http://docs.python.org/library/json.html for more info.
Videos
07:00
Python Pretty Print JSON String: Enhance Readability with Proper ...
06:10
Pretty Print JSON in Your Terminal with jq or Python - YouTube
Pretty Printing JSON data using Python
01:10
How to pretty print a JSON file in Python - YouTube
01:17
Python Shorts - json.tool utility for pretty-printing JSON on the ...
11:28
JSON in Python - Writing to Files - YouTube
DigitalOcean
digitalocean.com › community › tutorials › python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 16, 2025 - There are several tools to automatically pretty-print JSON in Python scripts. Here are a few options: You can use json.tool in the terminal to pretty-print JSON from a file or standard input:
Reddit
reddit.com › r/learnpython › formatting json output in python
r/learnpython on Reddit: formatting json output in Python
May 12, 2022 -
Hi,
I would like to read json into Python code, and then output processed json. In order to get started with this, I have written very basic Python, and am attempting to read in very basic json I found online.
The input json is:
{
"firstName": "John",
"lastName": "Doe",
"hobbies": ["biking", "coding", "rapping"],
"age": 35,
"children": [
{
"firstName": "hector",
"age": 6
},
{
"firstName": "cassandra",
"age": 8
}
]
}The code is:
import json
if __name__ == '__main__':
print( "start" )
# read and load input json
json_input_filename = "input.json"
json_input = open( json_input_filename )
json_input_dict = json.load( json_input )
# write output json
json_output_filename = "output.json"
with open( json_output_filename, 'w' ) as json_output:
json.dump( json_string, json_output )
print( f"end" )and the output is:
"{\"firstName\": \"John\", \"lastName\": \"Doe\", \"hobbies\": [\"biking\", \"coding\", \"rapping\"], \"age\": 35, \"children\": [{\"firstName\": \"hector\", \"age\": 6}, {\"firstName\": \"cassandra\", \"age\": 8}]}"What can I do in order to preserve something resembling the original formatting? I'm going to load this output into some other code in order to process it further.
Thank you very much
PythonHow
pythonhow.com › how › prettyprint-a-json-file
Here is how to prettyprint a JSON file in Python
import json # Load the JSON data from a file with open('your_file.json') as file: data = json.load(file) # Pretty print the JSON data pretty_json = json.dumps(data, indent=4) # Print or save the pretty printed JSON print(pretty_json) ... In this example, you need to replace 'your_file.json' ...
GeeksforGeeks
geeksforgeeks.org › python › pretty-print-json-in-python
Pretty Print JSON in Python - GeeksforGeeks
July 12, 2025 - Note: For more information, refer to Read, Write and Parse JSON using Python · Whenever data is dumped into Dictionary using the inbuilt module "json" present in Python, the result displayed is same as the dictionary format. Here the concept of Pretty Print Json comes into picture where we ...
Delft Stack
delftstack.com › home › howto › python › how to pretty print a json file
How to Pretty Print a JSON File in Python | Delft Stack
March 11, 2025 - This tutorial introduces how to pretty print a JSON file in Python. Learn different methods to enhance the readability of your JSON data, including using the built-in json module, reading from files, and command-line tools. Improve your programming skills and make your JSON files easier to navigate.
iO Flood
ioflood.com › blog › python-json-pretty-print
Python JSON Pretty Print | Guide (With Examples)
February 1, 2024 - Additionally, when working with APIs, you’ll often need to handle JSON data. APIs typically return data in JSON format, and pretty printing can help you understand the data you’re receiving. Python’s JSON module also allows you to read and write JSON data to files.