Home Code How to pretty print a JSON file in Python?

How to pretty print a JSON file in Python?

Pretty printing makes a json file visually appealing and easy to edit. If you have a JSON file that is compact or not visually appealing, you can easily convert it in Python to a pretty print form.

Pretty Print function in Python

Below is a function that converts any json information into its pretty print version. You can also optionally sort the output by keys or change the pretty print indent size. The function automatically identifies the type of the json data which means you can either pass a json file path, raw json string or a python dictionary, and the function will process it accordingly.

%MINIFYHTML395f9d5ea2da09e9869be77a6bd314e143%
import json def make_json_pretty(json_info,indent=4,sort=True): if type(json_info) is str: #if json_info is a string if json_info.strip()[0]=="{": #if json_info is raw json data json_data=json.loads(json_info) else: #if json_info is file path with open(json_info) as json_file: json_data = json.load(json_file) elif type(json_info) is dict: #if json_info is a python dictionary json_data = json_info else: #if json_info is not json data or file path raise Exception("first parameter should be JSON file path or JSON data") pretty_json = json.dumps(json_data, indent=4, sort_keys=sort) return pretty_json

Pretty Print via Command Line in Python 3

Starting from Python 3, you can pretty print a json file right from the command line! See example below.

python3 -m json.tool < path_to_my_json.json

The above command will pretty print the JSON data in the console itself. Instead, if you want to save the pretty print version to another file redirect the output as follows.

python3 -m json.tool < path_to_my_json.json > path_to_pretty.json
Content Protection by DMCA.com
Gurudevhttps://www.hitxp.com
Gurudev is the developer of Gurunudi AI Platform. This is his official website where he pens his thoughts on a wide range of topics, answers queries, shares resources and tools developed by him.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FacebookLike
InstagramFollow
PinterestFollow
RSS FeedSubscribe
Sound CloudFollow
TwitterFollow
YoutubeSubscribe

Latest Articles

Online Education should be interactive and innovative – Fun to Learn

For online education to succeed for smaller classses - it should be as interesting as cartoons. Online education cant succeed unless it is more interesting, interactive and innovative than offline schooling.

The Complete List of Dhatus – Sanskrit Root Words and their English meaning

A complete list of all dhatus (root words or verb roots) in Sanskrit dictionary and their meanings with corresponding IPA transliteration.

Sanskrit Lesson 4 – Word Creation Magic using Dhatus

Learn how easy it is to create new words in Sanskrit using root words called Dhatus. This simple process of Sanskrit grammar powers the entire language.

The Secret Role of Karma in Mahabharata

The seeds of Mahabharata were sown long back Duryodhana or Dhritarashtra were born. The story of Kaurvas and Pandavas is only the second half of the cycle of Karma. So what is the first half?

Latest Music Notations

So Gaya Ye Jahan – Tezaab – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song So Gaya Ye Jahan from the 1988 Hindi movie Tezaab in Western and Indian Notations.

Background Theme (BGM) – Mouna Ragam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Background Theme (BGM) from the 1986 Tamil movie Mouna Ragam in Western and Indian Notations.

Dil Kya Kare Jab Kisi Se – Julie – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Dil Kya Kare Jab Kisi Se from the 1975 Hindi movie Julie in Western and Indian Notations.

Albela Sajan Aayo Re – Hum Dil De Chuke Sanam – Piano Notations

Piano, Keyboard, Violin, Flute notes, Guitar Tabs and Sheet Music of the Song Albela Sajan Aayo Re from the 1999 Hindi movie Hum Dil De Chuke Sanam in Western and Indian Notations.
Content Protection by DMCA.com