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.
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)
How to output preview of JSON file data structure?
Have you considered using pprint from pprint module? It eases the printing on the eyes. Then I would just do pprint(data) More on reddit.com
How do I pretty-print JSON in Python?
Pass the indent parameter to json.dumps(), for example json.dumps(data, indent=2). This formats the JSON across multiple lines with 2-space indentation so it's easy to read. Without indent, json.dumps() puts everything on a single compact line.
jsontotable.org
jsontotable.org › blog › python › python-pretty-print-json
Python Pretty Print JSON - Format JSON with Indentation (2025) ...
How do I sort keys when pretty-printing JSON in Python?
Add sort_keys=True, for example json.dumps(data, indent=2, sort_keys=True). This orders the keys alphabetically, which produces consistent output every run and makes JSON files much easier to compare in version control.
jsontotable.org
jsontotable.org › blog › python › python-pretty-print-json
Python Pretty Print JSON - Format JSON with Indentation (2025) ...
What is the difference between json.dumps() and pprint for printing JSON?
json.dumps(data, indent=2) produces a real JSON string with double quotes and JSON syntax, which is what you want for output, files, or APIs. The pprint module formats a Python object using Python syntax (single quotes, True/None), so it's handy for debugging but isn't valid JSON. For anything JSON-related, use json.dumps().
jsontotable.org
jsontotable.org › blog › python › python-pretty-print-json
Python Pretty Print JSON - Format JSON with Indentation (2025) ...
11:28
JSON in Python - Writing to Files - YouTube
11:16
Pretty Printing JSON data using Python
01:10
How to pretty print a JSON file in Python - 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
PythonHow
pythonhow.com › how › prettyprint-a-json-file
Here is how to prettyprint a JSON file in Python
New: Practice Python, JavaScript & SQL with AI feedback — Try ActiveSkill free → × ... To pretty print a JSON file in Python, you can use the json module along with the json.dumps() function.
EyeHunts
tutorial.eyehunts.com › home › python write json to file pretty
Python write JSON to file pretty
January 26, 2023 - Simple example code Pretty Printed JSON formatted data into a file. import json student = { "id": 1, "name": "John Duggar", "class": 9, "attendance": 75, "subjects": ["English", "Geometry"], "email": "[email protected]" } with open("stu.json", "w") as write_file: json.dump(student, write_file, indent=4)
Jsontotable
jsontotable.org › blog › python › python-pretty-print-json
Python Pretty Print JSON - Format JSON with Indentation (2025) | JSON to Table Converter
January 16, 2025 - Quick Answer: Use json.dumps(data, indent=2) to pretty print JSON with 2-space indentation in Python.
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.
pythontutorials
pythontutorials.net › blog › how-to-prettyprint-a-json-file
How to Pretty-Print a JSON File in Python: Step-by-Step Tutorial with Code Examples — pythontutorials.net
(Optional) Write the pretty-printed JSON back to a new file. Example: Read from input.json and Write to output.json ... {"name":"John Doe","age":30,"hobbies":["reading","gaming"],"address":{"city":"Anytown","zip":"12345"}} ... import json # Read the minified JSON file with open("input.json", "r") as infile: data = json.load(infile) # Loads JSON into a Python dict # Pretty-print and write to a new file with open("output.json", "w") as outfile: json.dump(data, outfile, indent=4, sort_keys=True) # Use json.dump() to write to file print("Pretty-printed JSON saved to 'output.json'")
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.
Gleske
sam.gleske.net › blog › engineering › 2017 › 10 › 21 › python-json-pretty-dump.html
Python outputting json.dump like json.tool
October 21, 2017 - To replicate json.dumps like json.tool ... json.dump like json.tool: import json with open('compact.json') as f: json_object = json.load(f) with open('pretty.json', 'w') as f: json.dump(json_object, f, indent=4, separators=(',', ': '), sort_keys=True) #add trailing newline for POSIX ...