Pretty-print JSON with Python

curl -s 'https://api.github.com/orgs/github/repos' | python -m json.tool

November 15, 2023bashoneliners


Consider the JSON output of a typical web API, for example GitHub's:

curl -s 'https://api.github.com/orgs/github/repos' | head -c 100

The first 100 characters look like this:


It's compact, without unnecessary spaces and line breaks, which makes it smaller and efficient for machines, but hard to read for humans.

When the non-standard jq tool is available, then making this readable is as easy as piping to jq ..

When jq is not available, a good and widely available alternative is Python's json tool:

curl -s 'https://api.github.com/orgs/github/repos' | python -m json.tool

It reads lines from standard input as JSON, and prints the content with line breaks, indentation and spaces to make it easier to read, for example the first 10 lines of the above would look like this:

        "id": 3222,
        "node_id": "MDEwOlJlcG9zaXRvcnkzMjIy",
        "name": "media",
        "full_name": "github/media",
        "private": false,
        "owner": {
            "login": "github",
            "id": 9919,