How to prettyprint a JSON file?

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

    I had a similar requirement to dump the contents of json file for logging, something quick and easy:

    print(json.dumps(json.load(open(os.path.join('', ''), "r")), indent = 4 ))
    

    if you use it often then put it in a function:

    def pp_json_file(path, file):
        print(json.dumps(json.load(open(os.path.join(path, file), "r")), indent = 4))
    

提交回复
热议问题