Python Pretty Print JSON With Examples

We can use the Python json module to pretty-print the JSON data.

The json module is recommended to work with JSON files. We can use the dumps() method to get the pretty formatted JSON string.

1. Python Pretty Print JSON String

Output:

  • First of all, we are using json.loads() to create the json object from the json string.
  • The json.dumps() method takes the json object and returns a JSON formatted string. The indent parameter is used to define the indent level for the formatted string.

2. Python Pretty Print JSON File

Let’s see what happens when we try to print a JSON file data. The file data is saved in a pretty printed format.

Python Pretty Print JSON

 

Json Pretty Printed File

Output:

It’s clear from the output that we have to pass the indent value to get the JSON data into a pretty printed format.

References

By admin

Leave a Reply

%d bloggers like this: