One fine morning, I was playing around with JSON, basically trying out few websites which allows us to pretty print them.
I thought to myself -
“Won’t it be cool to pretty print JSON in terminal itself?”
So I decided to write a command line tool for this. But then I made a mistake. I did a google search to check if an existing command line tool built for this exact same purpose, already exists or not.
Why a mistake, you ask? Well, this made me put this idea of building this tool on hold for a while. That’s why. Hehe. […..…lame excuse……]
Anyway, long story short, I found a neat tool to serve our purpose. Before we get to that, let’s first refresh our memory a bit about JSON.
Shall we? Let’s go!
What is JSON?
Short for “JavaScript Object Notation“
Most widely used (and loved) form of transferring data between computer systems
It is a set of key - value pairs, enclosed in curly braces
Looks like this:
What do we developers do most of the time?
I meant to ask - What do developers do most with JSON data? 😛
We try to read it, understand the data and try to find specific keys and their values.
While transferring data between computer systems, we send and receive JSON data in serialised form. To understand this in layman terms, imagine this data coming to us in the form of long single text line. What happens if all the content is present in a single line like this?
If the data is very large, then this line will be very long, and it will be very hard for us to find relevant data inside this json text.
That’s why, we use JSON formatter sites to pretty print this JSON text. Pretty print, as the name suggests, means “printing the JSON in proper form with appropriate line breaks and proper indentation”, as shown in the image present in first section of this blog post.
How to pretty print this in terminal?
There is a command line tool called jq, using which you can pretty print your json data within your terminal window. 🔥 (official page - link)
It can be downloaded and installed via brew: brew install jq
To use it: echo '{…….}' | jq
If you are curious to learn more about JSON, do check out this fun and interesting talk by Douglas Crockford here: