If you ever deal with JSON API requests through the terminal with cURL, you might be frustrated with how the resulting data is rendered. In a normal API call (in this case, the Chuck Norris Database):

curl http://api.icndb.com/jokes/random

…there is no formatting at all, see:

JSON with no formatting
JSON with No Formatting

In order to get your JSON responses to print out in the Terminal prettily formatted, you can pipe (using the | character) the response into Python using the following command:

curl http://api.icndb.com/jokes/random | python -m json.tool

Resulting in your JSON being printed out all pretty-like:

JSON with pretty formatting
JSON with Pretty Formatting

To make this even more efficient, you can make a script that lets you just type the following:

curl http://api.icndb.com/jokes/random | json

…to get the same result, check it out:

JSON with pretty formatting short command
JSON with Pretty Formatting with Short Command

Just follow along these three simple steps if you want to do the same.

1. Create the Script

Open up your favorite plain text editor, and write the following:

#!/bin/bash

python -m json.tool

Then save the file to your desktop as json, without any extensions. Like this:

json file
json File

Note: Many text editors will suggest an extension such as .txt when saving, but in this case, we want no extension. Common file extensions are often hidden by default by your operating system, so you need to make sure your file actually has no extension, by pressing cmd + i on the file to bring up the Info panel and check the Name & Extension section to make sure the file is just called json.

json
json

2. Make File Executable

Next, you need to make the json file executable, so first, navigate to your Desktop (or wherever you saved the file) in Terminal:

cd ~/Desktop

Then, use the following command:

chmod +x json

If you check the icon now, you’ll see it has turned into a Unix Executable:

json Unix Executable
json Unix Executable

3. Move File to Path

Finally, you need to put the file somewhere your system knows to look, this is known as your Path. You can check all the directories in your path by using the following command:

echo $PATH
Path
My Path

Note: The contents of your path will be different depending on what you have installed on your system.

In this example, you will place the json executable you just created into the /usr/local/bin/ directory with the mv (move) command:

mv json /usr/local/bin/
move json
Move json File to Path

Just press enter and Boom, job done!

You can now simply use:

| json

…after your API request to make your JSON have pretty formatting in your Terminal.