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

Avatar

By squashlabs, Last Updated: Nov. 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 Convert a Dictionary To JSON In Python

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.

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

How To Remove Whitespaces In A String Using Python

Removing whitespaces in a string can be done easily using Python. This article provides simple and effective methods for removing whitespace, includi… read more

How to Adjust Font Size in a Matplotlib Plot

Adjusting font size in Matplotlib plots is a common requirement when creating visualizations in Python. This article provides two methods for adjusti… read more

How to Use and Import Python Modules

Python modules are a fundamental aspect of code organization and reusability in Python programming. In this tutorial, you will learn how to use and i… read more

How to Check If a Variable Exists in Python

Verifying the existence of a variable in Python code is a fundamental skill for any programmer. This article provides a simple guide on how to check … read more

How to Define a Function with Optional Arguments in Python

Defining functions with optional arguments in Python is a valuable skill for any developer. This article provides a simple guide to understanding the… read more

How To Check If Key Exists In Python Dictionary

Checking if a key exists in a Python dictionary is a common task in programming. This article provides simple examples and explanations on how to per… read more

How To Change Matplotlib Figure Size

Learn how to adjust the size of figures drawn using Matplotlib in Python for better visualization and presentation. This article will guide you throu… 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 t… read more

How to Download a File Over HTTP in Python

Guide on using Python to download a file from a URL via HTTP. Learn how to download files using the requests library and the urllib module. Best prac… read more

How To Install OpenCV Using Pip

Installing OpenCV using pip in Python is a process that allows you to utilize this powerful computer vision library for your projects. This article p… read more