A Newb Learns Python

Python: For all your JSON pretty printing needs

Why use a website when Python can pretty print json right in your terminal…

>>> cat search.json
{"responseHeader":{"status":0,"QTime":0},"response":{"numFound":86295,"start":0,"docs":[{"artist":"Explosions in the Sky","id":"6-1010373","image":"67660114.jpg","reach":768165,"resid":1010373,"restype":6,"weight":1.0},{"artist":"Skid Row","id":"6-1000620","image":"796810.jpg","reach":515805,"resid":1000620,"restype":6,"weight":1.0}]}}

>>> cat search.json | python -m json.tool
{
    "response": {
        "docs": [
            {
                "artist": "Explosions in the Sky", 
                "id": "6-1010373", 
                "image": "67660114.jpg", 
                "reach": 768165, 
                "resid": 1010373, 
                "restype": 6, 
                "weight": 1.0
            }, 
            {
                "artist": "Skid Row", 
                "id": "6-1000620", 
                "image": "796810.jpg", 
                "reach": 515805, 
                "resid": 1000620, 
                "restype": 6, 
                "weight": 1.0
            }
        ], 
        "numFound": 86295, 
        "start": 0
    }, 
    "responseHeader": {
        "QTime": 0, 
        "status": 0
    }
}

Is the JSON already in your clipboard? No need to put it in a file, just use the builtin “pbpaste” command-line tool.

>>> pbpaste | python -m json.tool
{
    "cool": "this was something I just copied in my browser", 
    "pbpaste": "is a neat little utility"
}

(Source: richardlog.com)

  1. pynewb posted this