How to Pretty Print a JSON File in Python (Human Readable)

Avatar

By squashlabs, Last Updated: November 11, 2023

How to Pretty Print a JSON File in Python (Human Readable)

To prettyprint a JSON file in Python, you can use the built-in json module. This module provides functions for working with JSON data, including parsing JSON strings into Python objects and serializing Python objects into JSON strings. The json module also provides the dump() and dumps() functions for prettyprinting JSON data.

Using the dump() function to prettyprint a JSON file

The dump() function from the json module allows you to write JSON data to a file while prettyprinting it. Here is an example of how to use dump() to prettyprint a JSON file:

import json

# JSON data
data = {
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

# Open a file for writing
with open("data.json", "w") as file:
    # <a href="https://www.squash.io/how-to-convert-json-to-csv-in-python/">Write JSON</a> data to the file with indentation
    json.dump(data, file, indent=4)

In this example, we create a dictionary data representing JSON data. We then open a file named data.json in write mode using the open() function. We pass the file object to the dump() function along with the JSON data and specify the indent parameter as 4 to indicate the desired indentation level. The dump() function writes the prettyprinted JSON data to the file.

Related Article: How to Use Infinity in Python Math

Using the dumps() function to prettyprint a JSON file

If you don’t need to write the prettyprinted JSON data to a file and just want to obtain a string representation, you can use the dumps() function. Here is an example:

import json

# JSON data
data = {
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

# Get the prettyprinted JSON string
pretty_json = json.dumps(data, indent=4)

# Print the prettyprinted JSON string
print(pretty_json)

In this example, we use the dumps() function to convert the data dictionary into a prettyprinted JSON string. We specify the indent parameter as 4 to indicate the desired indentation level. The resulting JSON string is assigned to the variable pretty_json, which can be used or printed as needed.

Alternative ideas

If you prefer a third-party library, you can use the pprint module from the pprint package. The pprint module provides a pprint() function that can be used to prettyprint JSON data. Here is an example:

from pprint import pprint

# JSON data
data = {
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

# Prettyprint the JSON data
pprint(data, indent=4)

In this example, we import the pprint function from the pprint module. We then pass the data dictionary to the pprint() function along with the indent parameter set to 4. The pprint() function prettyprints the JSON data to the console.

Best practices

When prettyprinting JSON data, it is common to use an indentation level of 4 spaces. This indentation level helps improve readability by visually separating nested elements. It is also important to ensure that the JSON data is properly formatted before attempting to prettyprint it. Invalid JSON data may result in errors or unexpected output.

Related Article: How to Execute a Program or System Command in Python

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

How to Use Python with Multiple Languages (Locale Guide)

Python locale is a powerful tool for managing cultural differences in your code. This complete guide covers everything you need to know, from the basics of using locale... read more

How to Suppress Python Warnings

Python warnings can clutter your code and make it harder to read. In this short guide, we'll show you two methods to suppress Python warnings and keep your code clean.... read more

How to Measure Elapsed Time in Python

Measuring elapsed time in Python is essential for many programming tasks. This guide provides simple code examples using the time module and the datetime module.... read more

How to Execute a Curl Command Using Python

Executing a curl command in Python can be a powerful tool for interacting with APIs and sending HTTP requests. This article provides a guide on how to execute a curl... read more

How to Parse a YAML File in Python

Parsing YAML files in Python can be made easy with the help of Python's yaml parser. This article provides a guide on how to parse YAML files using the PyYAML and... read more

How to Automatically Create a Requirements.txt in Python

Managing dependencies in Python is crucial for smooth software development. In this article, we will explore two methods to automatically create a requirements.txt file,... read more