Monday, January 02, 2017

jq - a powerful unix command line tool for handling JSON data

Nowadays, JSON is a popular data format, especially in web services domain. However, traditionally, Unix command line tools, e.g. sed, awk, grep, etc., are not capable to handle JSON data format. So, it is hard to write shell scripts to reading JSON data.
Fortunately, I found a handy and powerful tool - jq. In this page, I want to show you an example of using jq to extract weather forecast in JSON format to CSV. First of all, I get the data from weather forecast serveice of api.openweathermap.org and save it to "forcecast.json" file:
Here is the raw data, hard to read by human,
jq can print the JSON in pretty format, with color:
Now, I show you how to extract the dt_txt(forecast time), temp (forecast temperature) and humidity (forecast humidity) from the JSON data to CSV:
Just a  single command can covert JSON to CSV. How powerful is it! For the usage details of jq, you can refer to the manual