How to prettyprint a JSON file?

前端 未结 13 983
滥情空心
滥情空心 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:18

    To be able to pretty print from the command line and be able to have control over the indentation etc. you can set up an alias similar to this:

    alias jsonpp="python -c 'import sys, json; print json.dumps(json.load(sys.stdin), sort_keys=True, indent=2)'"
    

    And then use the alias in one of these ways:

    cat myfile.json | jsonpp
    jsonpp < myfile.json
    

提交回复
热议问题