How to prettyprint a JSON file?

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

    Here's a simple example of pretty printing JSON to the console in a nice way in Python, without requiring the JSON to be on your computer as a local file:

    import pprint
    import json 
    from urllib.request import urlopen # (Only used to get this example)
    
    # Getting a JSON example for this example 
    r = urlopen("https://mdn.github.io/fetch-examples/fetch-json/products.json")
    text = r.read() 
    
    # To print it
    pprint.pprint(json.loads(text))
    

提交回复
热议问题