JSON is a very popular platform independent data format. One of the great benefits of working with JSON is that it is generally easy to read. However, reading JSON objects becomes more difficult as the objects become large, especially on the command line. Pretty printing JSON records on the command line makes reading it much easier.

Pretty printing JSON records on the command line is easy to do with jq. jq is a lightweight command line JSON processor. jq is easy to install on Linux, Mac, and Windows, and can easily be used with other command line utilities such as sed, awk, grep, etc.

To simply pretty print JSON with jq, use the . filter. This filter just takes input and outputs it unchanged. jq by default will pretty print all output.

Here is an example using jq to pretty print a JSON object:

#!/bin/bash

echo '{"url" : "http://bigdatums.net", "name": {"first": "big", "last": "datums"}, "id": 51230}' | jq '.'
{
  "url": "http://bigdatums.net",
  "name": {
    "first": "big",
    "last": "datums"
  },
  "id": 51230
}

For more information about jq, please visit the the official site https://stedolan.github.io/jq/.

Leave a Reply

How to Pretty Print JSON on the Command Line