Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I have a JSON file that is a mess that I want to prettyprint-- what's the easiest way to do this in python? I know PrettyPrint takes an "object", which I think can be a file, but I don't know how to pass a file in-- just using the filename doesn't work.

2 Answers

0 votes
by (106k points)

For writing a JSON file in a good manner way, you can use json.load():

Below is the piece of code that can be used with a JSON file:-

with open('filename.txt', 'r') as handle:

parsed = json.load(handle)

Another way you can follow, you can use the following command:-

python3 -m json.tool < some.json

0 votes
by (108k points)

In your case, you can also use the built-in module named pprint from Python:

Refer to the below code that will help you to read the file with json data and print it out.


import json

import pprint

json_data = None

with open('file_name.txt', 'r') as f:

    data =

    json_data = json.loads(data)


Related questions

0 votes
1 answer
asked Jul 2, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
asked Dec 12, 2020 in Python by laddulakshana (16.4k points)
0 votes
2 answers
asked Oct 4, 2019 in Python by Tech4ever (20.3k points)
0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (16.4k points)

Browse Categories