< BACK

Bash tips, tricks and code snippets

Bash function to pretty-print JSON

Define a one-line function ppj (which can be saved to ~/.bashrc):

ppj() { cat "$1" | python -m json.tool; }

so now a file test.json containing following json:

{"status": "confirmed", "kind": "calendar#event", "end": {"timeZone": "Etc/GMT", "dateTime": "2017-04-20T11:45:00+02:00"}, "description": "something"}

via the command

ppj test.json

or echoing the string directly, like this:

ppj <(echo '{"status": "confirmed", "kind": "calendar#event", "end": {"timeZone": "Etc/GMT", "dateTime": "2017-04-20T11:45:00+02:00"}, "description": "something"}')

will be output like this:

{
    "description": "something",
    "end": {
        "dateTime": "2017-04-20T11:45:00+02:00",
        "timeZone": "Etc/GMT"
    },
    "kind": "calendar#event",
    "status": "confirmed"
}