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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pretty-print-json
Python - Pretty Print JSON - GeeksforGeeks
July 23, 2025 - To write a Python object as JSON Pretty Print format data into a file, json.dump() method is used.
Discussions

formatting json output in Python
json.dumps(obj, indent=2, sort_keys=False) More on reddit.com
🌐 r/learnpython
16
52
May 12, 2022
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
🌐 r/learnpython
18
10
November 28, 2022
Printing pretty JSON from a Dict
from pprint import pprint

pprint(whatever)
More on reddit.com
🌐 r/pythontips
4
5
June 5, 2016
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
🌐 r/learnpython
7
13
December 20, 2021
People also ask

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) ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-pretty-print-json
How to Pretty Print JSON in Python | DigitalOcean
September 15, 2025 - Learn how to pretty print JSON in Python using built-in tools like json.dumps() and pprint to improve readability and debug structured data efficiently.
🌐
PYnative
pynative.com › home › python › json › python prettyprint json data
Python PrettyPrint JSON Data
May 14, 2021 - Python Write Indented and Pretty-printed JSON into a file. Prettyprint JSON file and JSON string. Use pprint module to pretty-print JSON. Pretty-print JSON from the command line
🌐
Better Stack
betterstack.com › community › questions › how-to-print-json-file-in-python
How to Prettyprint a Json File in Python? | Better Stack Community
You can pretty print a JSON file in Python using the json module's loads() function to parse the JSON data, and then the json.dumps() function with the indent parameter to output it in a readable format.
🌐
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.
Find elsewhere
🌐
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)
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to prettyprint a json file with python?
How to PrettyPrint a JSON File Using Python?
December 13, 2022 - This guide shows different ways to PrettyPrint the JSON file format using Python. ... Python 3 installed and configured. An IDE or text editor to write the code.
🌐
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.
🌐
Codingem
codingem.com › home › python pretty print json
Python Pretty Print JSON [with 3 Examples] - codingem.com
January 23, 2023 - Python pretty-printed JSON has indentations, spacings, and separators for your convenience. To pretty-print, call json.dumps(indent,separator)
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to pretty print a json file in python?
How to Pretty Print a JSON file in Python? - Spark By {Examples}
May 31, 2024 - The dumps() from the json module & pprint() from the pprint module are used to pretty print a JSON file in Python, JSON data is often stored in a
🌐
datagy
datagy.io › home › python posts › pretty print a json file in python (6 methods)
Pretty Print a JSON File in Python (6 Methods) • datagy
August 31, 2022 - We use the with keyword to create a writeable file, where we then dump our object into, using the indent= keyword · The context manager will handle closing the file once it’s done saving the file for us.
🌐
Python Guides
pythonguides.com › python-pretty-print-json
How to Pretty Print JSON in Python
August 12, 2025 - The sort_keys=True parameter is particularly helpful when I need consistent output for version control or comparing JSON files. Python’s pprint module is another excellent option for pretty printing JSON data.
🌐
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'")
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
July 21, 2026 - You write JSON with Python using json.dump() to serialize data to a file. You can minify and prettify JSON using Python’s json.tool module.
🌐
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.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - This module provides a dumps() function that can serialize Python objects into a JSON formatted string. By default, this function produces a JSON string without any formatting, but we can use the indent parameter to specify the number of spaces ...
🌐
HowToDoInJava
howtodoinjava.com › home › python json › python – write json to a file
Python - Write JSON to a File - Write dict to a File
October 2, 2022 - import json # Python dict py_dictionary ... "Lokesh", "Age": 39, "Blog": "howtodoinjava"} For pretty printing, use indent parameter of the method dump()....
🌐
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 ...