How to pretty print JSON files in Python

Play this article

Preface📃

JSON is a powerful data format that machines and API endpoints use to talk to each other with.

But JSON in its raw form can be difficult to read and understand at times, especially the more robust ones.

There is a programmatic solution to dealing with this - pretty print.

What is pretty printing with JSON?🤔💭

Pretty printing is just displaying data in a format that is visually appealing or easy to read. From a JSON perspective, this makes it easier to

  • create neater JSON snippets

  • identity key-value pairs within the data

  • see the hierarchical structure of the JSON data

Python provides us with the utility to do this using the in-built json module.

You can pretty print JSON from

  • strings

  • files

  • APIs

  • databases

  • streaming pipelines

  • 3rd party services

Let’s explore how to pretty print JSON strings and files:

A. Pretty-print the JSON strings🧶

A JSON string is a serialized version of JSON data. In Python, they are usually deserialized into a dictionary to represent data in the real world.

1. Import the json module

You don’t need to install this library on your machine or virtual environment. Simply add this to your script:

import json

2. Load your JSON data

Let’s create a JSON string:

string_data = '{"food": "Italian BMT", "price": 5.99, "store_branch": "London"}'
print(string_data)

Output:

{"food": "Italian BMT", "price": 5.99, "store_branch": "London"}

3. Parse the JSON string

Convert the JSON string to a Python dictionary:

json_data = json.loads(string_data)

Output:

{'food': 'Italian BMT', 'price': 5.99, 'store_branch': 'London'}

4. Pretty print the JSON string

Now let’s print the formatted JSON into a visually engaging format:

pretty_json_data = json.dumps(json_data, indent=4)
  • The dumps function converts the JSON string into a formatted one

  • The indent operation indents the data to the degree provided - this is the key parameter controlling how clean the JSON outputs are, finetuning this to the right number will create neat, presentable outputs

Output:

{
    "food": "Italian BMT",  
    "price": 5.99,
    "store_branch": "London"
}

B. Pretty-print the JSON files📁

Let’s pretend we have a file called “food_menu.json” saved in a “src” directory:

[{"food": "Tuna & Sweetcorn Sandwich", "price": 3.99, "shop_location": "London"},
{"food": "Chicken Caesar Salad", "price": 5.49, "shop_location": "London"},
{"food": "Vegan Wrap", "price": 4.59, "shop_location": "Manchester"},
{"food": "Spicy Beef Panini", "price": 6.29, "shop_location": "Birmingham"},
{"food": "Mushroom Soup", "price": 2.89, "shop_location": "Liverpool"},
{"food": "Cheese & Tomato Pizza", "price": 7.99, "shop_location": "Leeds"}]

1. Load your JSON file

Assuming the json module is imported, combine a context manager with the load function to read your file from the source location:

with open("src/food_menu.json", "r") as file:
    menu = json.load(file)

2. Pretty print and save to a new file

Use another context manager and the dump function to write the pretty outputs to your target location:

with open("tgt/food_menu.json", "w") as file:
    json.dump(menu, file, indent=4)

Now you can convert raw JSON files and strings into more digestible and appealing formats!