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.
🌐
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
import json # Load JSON data from a file with open('file.json', 'r') as file: json_data = json.load(file) # Pretty print the JSON data pretty_json = json.dumps(json_data, indent=4) print(pretty_json) ...
🌐
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.
🌐
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()....
🌐
Python
docs.python.org › 3 › library › json.html
JSON encoder and decoder — Python 3.14.3 documentation
Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using this Python-to-JSON conversion table.
🌐
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
Find elsewhere
🌐
Real Python
realpython.com › python-json
Working With JSON Data in Python – Real Python
August 20, 2025 - 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.
🌐
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' ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-pretty-print-json-in-python
How to Pretty Print JSON in Python
April 14, 2023 - We will also cover best practices used to pretty print JSON, and also talk about it's use cases. In Python, "pretty print" refers to formatting and presenting data structures such as lists, dictionaries, and tuples in a more readable and organized way.
🌐
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.
Top answer
1 of 1
1
Explain Python Pretty Print JSONJSON (JavaScript Object Notation) is a widely used data format for data interchange. 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" functionality comes into play.Pretty printing is the process of formatting JSON data to make it more legible and visually appealing to humans. Python provides a built-in module called `json` that includes a `dumps()` method. Using this method with specific parameters lets you easily print JSON data.In this article, we'll explore using Python's `json.dumps()` method to pretty print JSON data.PrerequisitesBefore we dive into pretty printing JSON in Python, ensure you have Python installed on your system. You can download it from the official Python website: https://www.python.org/downloads/What is JSON?JSON is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It's often used to transmit data between a server and a web application or between different parts of an application.JSON data is represented as a collection of key-value pairs, similar to Python dictionaries. Here's a simple example:json{    "name": "John Doe",    "age": 30,    "city": "New York"}In Python, JSON data is typically converted to dictionaries or lists using the `json` module, making it easy to work with.Using `json.dumps()` for Pretty PrintingPython's `json.dumps()` function converts a Python object into a JSON formatted string. By default, the JSON output is compact and not very human-readable. You can use the `indent` parameter to make it more readable.Here's the basic syntax of `json.dumps()` with the `indent` parameter:import jsonpretty_json = json.dumps(your_data, indent=4) `your_data`: This is the Python object (e.g., dictionary or list) that you want to convert to JSON. `indent=4`: This parameter specifies the number of spaces to use for indentation in the resulting JSON string. In this case, we use 4 spaces to make it nicely formatted.Example: Pretty Printing JSONLet's see an example of pretty printing JSON in Python:import json# Sample JSON datadata = {    "name": "John Doe",    "age": 30,    "city": "New York",    "skills": }# Pretty print the JSON datapretty_json = json.dumps(data, indent=4)# Print the pretty JSONprint(pretty_json)When you run this code, it will produce the following nicely formatted JSON output:json{    "name": "John Doe",    "age": 30,    "city": "New York",    "skills": }As you can see, the JSON data is now structured with proper indentation, making it much easier to read and understand.Use Cases for Pretty PrintingPretty printing JSON is particularly helpful in scenarios where: Debugging: When you're working with JSON data and need to debug or inspect it, pretty printing makes it more human-readable and helps you identify issues more easily. Logging: If you're logging JSON data in your application, pretty print formatting can make your log files more organized and user-friendly. Configuration Files: Pretty printing is beneficial when dealing with configuration files in JSON format. It ensures that configuration settings are neatly organized. API Development: When building or consuming JSON APIs for data exchange, pretty printing can assist in documenting and testing the API endpoints.ConclusionPython's `json.dumps()` method with the `indent` parameter is a valuable tool for pretty much printing JSON data. It allows you to format JSON in a human-readable way, making it easier to work with, debug, and understand. Whether you're developing web applications, working with configuration files, or dealing with API data, pretty printing JSON can significantly improve your workflow and code readability.
🌐
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.
🌐
Stephen's blog
stephendavidwilliams.com › how-to-pretty-print-json-files-in-python
How to pretty print JSON files in Python
August 1, 2023 - with open("src/food_menu.json", "r") as file: menu = json.load(file) Use another context manager and the dump function to write the pretty outputs to your target location:
🌐
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 - Learn how to use Python to pretty print a JSON object, including from a file, from an API, and how to save the pretty output to a file.
🌐
Studytonight
studytonight.com › python-howtos › how-to-pretty-print-a-json-file-in-python
How to Pretty Print a JSON file in Python - Studytonight
It pipes the data to Python and applies the JSON tool. ... In this article, we learned how to pretty print a JSON file using json.dumps() function. We can even increase the value of the indent keyword to see changes in the output.