jq: How to Pretty Print JSON

Updated

Learn how to pretty print and format JSON in the terminal with jq

jq: How to Pretty Print JSON

Before we start, make sure you have jq installed on your computer. On a Mac, you can install it with Homebrew:

brew install jq

First, open your terminal and navigate to the folder with your JSON file. To pretty print it, run:

jq '.' data.json

Just replace data.json with your file. The . is the identity filter: it passes the whole document through unchanged, and jq prints it with two-space indents, one key per line, and colours. Don't leave the filter out, though. If you run jq data.json, jq treats the file name as the filter and you get an error like data/0 is not defined.

To sort the keys alphabetically, add -S. For wider indents, use --indent 4 or --tab.

To save the formatted version, redirect it to a new file:

jq '.' messy.json > pretty.json

jq leaves out the colours when it writes to a file, so pretty.json is clean. Open it to make sure everything looks right.

jq also works on piped text, so you can pipe an API response straight into it: curl -s https://api.example.com/data | jq '.'.