Pretty Print JSON in the Terminal
How to pretty print JSON on the command line
I was testing out a web API quickly with curl and it printed minified JSON to the terminal.
Instead of trying to read that densely packed json text, this is how to pretty print the json on the terminal instead.
The tool jq is designed to handle and format JSON data on the command line.
Here’s how you can do it:
Install
Install it using homebrew:
brew install jq
Usage
Use the following command to pretty-print the JSON response from curl
:
curl -s <URL> | jq .
-s
: This option makescurl
operate in silent mode (no progress meter or error messages).<URL>
: Replace this with the URL you are making the request to.| jq .
: This pipes the output ofcurl
tojq
, which formats the JSON. (the.
is an argument)
Example
curl -s https://api.example.com/data | jq .
This will print the JSON response in a readable, pretty-printed format.
Notes
jq is actually meant not to pretty print json, but to transform and manipulate json data.
The .
argument to it tells it to return the input unchanged as the output.
This implies (correctly) other arguments change the input json before outputting it.
A fully tutorial on it's abilities is available on it's website: jqlang.github.io/jq/tutorial/