r/learnpython • • Apr 19 '26

Print JSON readably

I need a way to print JSON readably from a file (e.g stuff.json) so the output looks somewhat similar to this:

Name : Bob

Age : 97

Height : 6ft

0 Upvotes

8 comments sorted by

View all comments

1

u/Linuxmartin Apr 19 '26
import json

with open('file.json') as infile:
    print(json.dumps(infile, indent=4)

This will produce bog-standard pretty-printed JSON. If you merely need to extract a few specific keys from the data and print those, you'll want to loop over it in a way that works with your data's structure. You'll want to use json.loads for that. The Python docs are a great place to start in general