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.
Explain Python Pretty Print JSON
It’s human-readable and machine-friendly, making it a popular choice for configuration files, APIs, and data storage. Sometimes, when working with JSON data in Python, you might encounter large or complex JSON structures that are challenging to read. That’s where Python’s “pretty print” ... More on accuweb.cloud
formatting json output in Python
json.dumps(obj, indent=2, sort_keys=False) More on reddit.com
json.dump doesn't write anything to the json file
Delete the dict.json you keep opening and finding empty, and then run your program. Did it get recreated? if not, it's probably writing the file to some other location the whole time. More on reddit.com
Printing pretty JSON from a Dict
from pprint import pprintMore on reddit.com
pprint(whatever)
Videos
11:28
JSON in Python - Writing to Files - YouTube
07:00
Python Pretty Print JSON String: Enhance Readability with Proper ...
06:10
Pretty Print JSON in Your Terminal with jq or Python - YouTube
01:10
How to pretty print a JSON file in Python - YouTube
11:16
Pretty Printing JSON data using Python
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:
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' ...
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
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.
Stack Abuse
stackabuse.com › reading-and-writing-json-to-a-file-in-python
Reading and Writing JSON to a File in Python
April 18, 2023 - If you encounter this edge-case, which has since been fixed in subsequent Python versions - try using json.dumps() instead, and write the string contents into a file instead of streaming the contents directly into a file. In this guide, we introduced you to the json.dump(), json.dumps(), json.load(), and json.loads() methods, which help in serializing and deserializing JSON strings. We've then taken a look at how you can sort JSON objects, pretty-print them, change the encoding, skip custom key data types, enable or disable circular checks and whether NaNs are allowed, as well as how to change the separators for serialization and deserialization.