Pretty Print JSON In Python

Table of Contents

Introduction

In this example I am going to show you how to pretty print JSON in Python programming language. The ugly JSON or one liner JSON data or string could be very difficult to read manually and that’s why you may need to prettify JSON data so that you could read it if anything goes wrong with the JSON data.

Related Posts:

Why do you need to prettify the JSON data?

You may need to read JSON data manually when you receive this JSON data as a response of a, for example, REST service. You want to find out the issue with JSON data which might have been passed as an input to a REST service. So an ugly one liner JSON data might be very difficult to read and keep a track of a particular key/value pair with a naked eye while you are reading. Sometimes you might need to share your JSON data to someone else when you are requested to do so.

Prerequisites

Python 3.9.7

Pretty Print JSON

Let’s say that you have the following JSON string as an input and you want the format of JSON in a standard structure to make the read more easier.

{"one":"AAA","two":["BBB","CCC"],"three":{"four":"DDD","five":["EEE","FFF"]}}

Or even you can use the following format:

{\"one\":\"AAA\",\"two\":[\"BBB\",\"CCC\"],\"three\":{\"four\":\"DDD\",\"five\":[\"EEE\",\"FFF\"]}}

The following code snippets will prettify the JSON data:

import json

#jsn = '{"one":"AAA","two":["BBB","CCC"],"three":{"four":"DDD","five":["EEE","FFF"]}}'
jsn = "{\"one\":\"AAA\",\"two\":[\"BBB\",\"CCC\"],\"three\":{\"four\":\"DDD\",\"five\":[\"EEE\",\"FFF\"]}}"

parsed_json = json.loads(jsn)
prettify_json = json.dumps(parsed_json, indent=4)
#prettify_json = json.dumps(parsed_json, indent=4, sort_keys=True)

print(prettify_json)

If you put third argument sort_keys=True in json.dump() function then the output will be in the ascending order of keys in JSON output. You can also change the indentation using the second argument indent.

The above code will produce the following output:

{
    "one": "AAA",
    "two": [
        "BBB",
        "CCC"
    ],
    "three": {
        "four": "DDD",
        "five": [
            "EEE",
            "FFF"
        ]
    }
}

Now if you sort keys in JSON output then the above output will have the sorted keys:

{
    "one": "AAA",
    "three": {
        "five": [
            "EEE",
            "FFF"
        ],
        "four": "DDD"
    },
    "two": [
        "BBB",
        "CCC"
    ]
}

Let’s say you want to read the JSON data from input JSON file. The input.json file has the following data:

{"one":"AAA","two":["BBB","CCC"],"three":{"four":"DDD","five":["EEE","FFF"]}}

To read the input JSON file and parse and write to an output JSON file (output.json), the following code snippet is given:

import json

with open('input.json', 'r') as inf:
    parsed_json = json.load(inf)
    prettify_json = json.dumps(parsed_json, indent=4)
    
    with open('output.json', 'w') as ouf:
        ouf.write(prettify_json)

The output.json file will have the following output:

{
    "one": "AAA",
    "two": [
        "BBB",
        "CCC"
    ],
    "three": {
        "four": "DDD",
        "five": [
            "EEE",
            "FFF"
        ]
    }
}

That’s all about how to pretty print JSON using Python.

Source Code

Download

Leave a Reply

Your email address will not be published.