Saturday, November 16, 2024
Google search engine
HomeGuest BlogsHow to PrettyPrint a JSON File with Python?

How to PrettyPrint a JSON File with Python?

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 FilePython: 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 contentsmy_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 PythonPrettyPrint 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 JSONOutput 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 pythonpprint 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 Pythonjson.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

RELATED ARTICLES

Most Popular

Recent Comments