Beautifully print json files using the Python command line

JSON is a very common data format, but difficult for humans to read. Some json files are just a very long line of text without any line breaks or spaces. It is difficult for humans to understand the content.

Python 2.6 and above versions provide json.tool which can print JSON files beautifully. This tool can convert JSON files into a form corresponding to attributes and values, which looks more comfortable (compared to a long line of text, seems much easier).

If you have a file called aaa.json (let's say in directory d:\temp) containing a bunch of JSON output with the following contents:

{"balance":0.5,"deinterlace_field_order":0,"deinterlace_mode":0,"enabled":true,"flags":0}

It can be formatted into a readable structure using Python at the command line as follows.

python -mjson.tool d:\temp\aaa.json

The screenshot of the running result is as follows:

It is also possible to write this output directly to another file, such as writing the result to the file d:\temp\bbb.json.

python  -m  json.tool  d:\temp\aaa.json  >  d:\temp\bbb.json

 

The content of the file d:\temp\bbb.json is as follows:

 

In this form, artificial looks feel much more comfortable.

Related references:

[1] Use Python to print beautiful JSON.   Use Python to print beautiful JSON - experience notes  .

 

Guess you like

Origin blog.csdn.net/Alexabc3000/article/details/127592430