jq JSON pretty print

Posted on Thursday, February 26, 2015


 

 Recently I have been using curl to test some RESTful service that return JSON data.  I wanted to pipe the output into something that would pretty print it similar to what you get from http://jsonlint.com/ [1].  I found ./jq http://stedolan.github.io/jq/ [2]

The jq tool can do a lot, in fact the describe it as a sed for JSON data.   I only want to use it to pretty print at the moment so I am going to show how to install it on Ubuntu 14.04, OS X via brew, and Cygwin; then how to use it to pretty print.



Installation


JQ's installation page http://stedolan.github.io/jq/download/ [3]

Ubuntu 14.04


Looks like in Ubuntu 14.04 it's pretty simple


     > sudo apt-get install jq


Then to see which version it is run the following


     > jq -V




It installed version 1.3






But, if you really want version 1.4 (the latest "stable" build as of 2/25/2015), you can do the following.   (I ran into some issues doing this, if you are going to do it read the entire thing first so you can run all the fixes at once)

Remove the currently installed jq


     > sudo apt-get remove jq



Clone the repository


     > git clone https://github.com/stedolan/jq.git




Checkout v 1.4 tag


     > cd jq
     > git checkout tags/jq-1.4




Build and install it


     > autoreconf -i


After  I ran autoreconf -i , I ran into an issue




Makefile.am:35: error: Libtool library used but 'LIBTOOL' is undefined.

I found this post http://stackoverflow.com/questions/18978252/error-libtool-library-used-but-libtool-is-undefined [4] .  Looks like I need to run libtoolize, which I first need to install libtool.


     > sudo apt-get install libtool




Now let me restart… Build and install (but run libtoolize first)


     > libtoolize
     > autoreconf -i
     > ./configure
     > make


After running make I got another error after running make.



/bin/bash: flex: command not found.

To fix this problem install flex


     > sudo apt-get install flex


Now re-run make


     > make






./config/ylwrap: line 176: yacc: command not found


To fix this problem install byacc


     > sudo apt-get install byacc


Now re-run make


     > make






Now I get this error. 

To fix this problem install bison


     > sudo apt-get install bison







I think that worked!

Now to install it run


     > sudo make install


Test it out


     > jq -V





As a side note you may need to install gcc (I had done this previously on this box) To do that run the following command.



    > sudo apt-get install gcc












Cygwin install

 (I ran into some issues doing this, if you are going to do it read the entire thing first so you can run all the fixes at once)



Before you start the cygwin install make sure you have the following installed

autoconf






automake




Don't forget to check Automake: wrapper for automake and aclocal


gcc-core





make










git







Clone the repository


     > git clone https://github.com/stedolan/jq.git


Checkout v 1.4 tag


     > cd jq
     > git checkout tags/jq-1.4





Build and install it


     > autoreconf -i


After  I ran autoreconf -i , I ran into an issue (same as before)



Makefile.am:35: error: Libtool library used but 'LIBTOOL' is undefined.





Run the Cygwin Setup and search for libtool



Install this package.


I found this post http://stackoverflow.com/questions/18978252/error-libtool-library-used-but-libtool-is-undefined [4] .  Looks like I need to run libtoolize, which I first need to install libtool.

Now let me restart… Build and install (but run libtoolize first)


     > libtoolize
     > autoreconf -i
     > ./configure
     > make


After running make I got another error after running make.



/bin/bash: flex: command not found.


Run the Cygwin Setup and search for flex



Install this package.

Now re-run make


     > make




./config/ylwrap: line 176: yacc: command not found

Run the Cygwin Setup and search for byacc



Install this package.



Also search for Bison



Install this package

I restarted at this point and ran these commands


     > libtoolize
     > autoreconf -i
     > ./configure
     > make





Now run make install


     > make install




Test it out


     > jq -V










OS X Brew


This assumes you have brew installed if you don't head over here http://brew.sh/ [7] and follow their install instructions.


Run this command to install it


     > brew install jq




And it's installed J

Test it out


     > jq -V




Brew does load version 1.4!







Testing it out


Now that it's all installed let's test it out.

I poked around http://www.data.gov/ [5] and found this JSON to test against.   I can open this page http://catalog.data.gov/dataset?res_format=JSON&_res_format_limit=0 which shows all the datasets which can send back JSON.








This led me to http://www.bls.gov/developers/api_unix.htm#unix2 [6] which lists a curl to access some of their data.  Then I found this page http://data.bls.gov/cgi-bin/surveymost?bls which lists some ID number for some interesting labor statistics.

I think LNS11000000 looks interesting.   I am going to curl it and pipe its output to jq


    > curl http://api.bls.gov/publicAPI/v2/timeseries/data/LNS11000000 | jq .


When piping json jq requies a "." after it to pretty print the entire JSON object.

Here are my results




Pretty cool J  Looks like we have 157 million people in the workforce as of January 2015.

I ran this on OSX, Ubuntu, and Cygwin.  All of them had the same results.





References

[1]        JSONLint
                        http://jsonlint.com/
                Accessed 2/2015
[2]        ./jq
                        http://stedolan.github.io/jq/
                Accessed 2/2015
[3]        ./jq installation page
                        http://stedolan.github.io/jq/download/
                Accessed 2/2015
[4]        error: Libtool library used but 'LIBTOOL' is undefined
                Accessed 2/2015
[5]        Data.gov
                        http://www.data.gov/
                Accessed 2/2015
[6]        Top Picks
                        http://data.bls.gov/cgi-bin/surveymost?bls
                Accessed 2/2015
[7]        Brew Home Page
                        http://brew.sh/
                Accessed 2/2015


3 comments:

  1. This page is fantastic - but it is missing a few steps for cygwin. You also need to install autoconf, automake, gcc-core and make.

    ReplyDelete
    Replies
    1. Thanks for the catch :) When doing these type of things I should always start a fresh raw cygwin and make sure I list what you need to install. I'm a little busy now but give me a a few days and I will test and update this post :)

      Delete
    2. Updated post and fixed the cygwin to show the pre-required tools you need installed :)

      Delete