How to PrettyPrint a JSON File with Python?

November 15, 2022

Introduction

JSON (JavaScript Object Notation) is a data format used in many applications and programming languages. The file format is compact and commonly appears in client-server applications, such as API responses.

When viewing JSON files in Python, PrettyPrint helps format the text in a visually pleasing format.

This guide shows different ways to PrettyPrint the JSON file format using Python.

Python: How To PrettyPrint A JSON File

Prerequisites

How to PrettyPrint JSON in Python?

Depending on the input and output format, there are various ways to PrettyPrint a JSON file using Python. Below is a list of different methods with steps.

Method 1: Using json

The json library contains all the necessary functions to work with JSON data. The json.dumps method automatically PrettyPrints parsed JSON data.

To see how it works, do the following:

1. Create a file named my_file.json with the following contents:

{"employees":[{"name":"bob","sector":"devops"},{"name":"alice","sector":"infosec"}]}
my_file.json contents

2. The code below demonstrates how to import and PrettyPrint a JSON file in Python:

import json

my_file = open("my_file.json")
loaded_json = json.load(my_file)

print(json.dumps(loaded_json, indent=2))
PrettyPrint JSON file Python

The code does the following:

  • Opens the file called my_file.json and saves it to the variable my_file. (Check out our guide on how to handle files in Python for more details.)
  • Loads the JSON into loaded_json using the json.loads method.
  • PrettyPrints the loaded JSON file using the json.dumps method. The indentation level is two spaces.

The output looks like this:

{
  "employees": [
    {
      "name": "bob",
      "sector": "devops"
    },
    {
      "name": "alice",
      "sector": "infosec"
    }
  ]
}
Output PrettyPrinted JSON

PrettyPrinting adds proper indentation for every nested object and spaces after commas (,) and colons (:).

Method 2: Using pprint

Use the pprint library to pretty print JSON files as strings. For example:

import json
import pprint

my_file = open("my_file.json")
loaded_json = json.load(my_file)

pprint.pprint(loaded_json)
pprint JSON python

The pprint method replaces double quotes with single quotes, which means the result is not in JSON format explicitly.

Note: Learn more about working with Python strings by referring to our article on how to substring a string in Python.

Method 3: Using the Terminal

Use the json.tool to PrettyPrint JSON files directly in the terminal. Use the following command format:

python3 -m json.tool my_file.json
json.tool terminal PrettyPrint Python

The JSON file contents print to the output with indentation level four.

Conclusion

After going through the methods in this guide, you know how to PrettyPrint a JSON file using Python in various ways.

Next, learn how to add items to a dictionary in Python.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
File Handling in Python: Create, Open, Append, Read, Write
February 24, 2022

Working with files is part of everyday tasks in programming. This tutorial teaches you elementary file...
Read more
Handling Missing Data in Python: Causes and Solutions
July 1, 2021

Some machine learning algorithms won't work with missing data. Learn how to discover if your dataset has missing...
Read more
Python Data Types {Comprehensive Overview}
April 1, 2021

Every programming language has their own set of built-in data types. In Python, data types provide information about a variable and what...
Read more
Introduction to Python Pandas
July 28, 2020

This tutorial introduces you to basic Python Pandas concepts and commands. A selection of clear-cut images and well-defined examples show you how...
Read more