How to prettyprint a JSON file?

前端 未结 13 808
滥情空心
滥情空心 2020-11-22 01:53

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

13条回答
  •  不思量自难忘°
    2020-11-22 02:05

    You could try pprintjson.


    Installation

    $ pip3 install pprintjson
    

    Usage

    Pretty print JSON from a file using the pprintjson CLI.

    $ pprintjson "./path/to/file.json"
    

    Pretty print JSON from a stdin using the pprintjson CLI.

    $ echo '{ "a": 1, "b": "string", "c": true }' | pprintjson
    

    Pretty print JSON from a string using the pprintjson CLI.

    $ pprintjson -c '{ "a": 1, "b": "string", "c": true }'
    

    Pretty print JSON from a string with an indent of 1.

    $ pprintjson -c '{ "a": 1, "b": "string", "c": true }' -i 1
    

    Pretty print JSON from a string and save output to a file output.json.

    $ pprintjson -c '{ "a": 1, "b": "string", "c": true }' -o ./output.json
    

    Output

提交回复
热议问题