jq is a command line JSON processor; with it you can map, filter, slice, and transform JSON. We will use jq with JSON much like we would use sed, awk, and grep with text. Since the AWS CLI tool returns JSON, we can use jq to parse the data. This can actually make things easier than with the older AWS command line tools that return text; rather than using grep and awk to find the rows and columns we are interested in, we can query for the specific attributes in JSON that we are looking for.

There are prebuilt binaries for Linux, Mac OS X, and Windows; however they are merely the executable, but they are adding packages for installation. You can check the Download jq to see if they have one for your platform yet or to get the prebuilt binaries. In this article we’ll look at building it from source.

JQ 1.3 and Later

In JQ 1.3, they went from a premade Makefile to using autotools. The old build instructions, and old article, are below: JQ 1.2 and Earlier.

The requirements are:

Bison

While you can probably get Bison as a package for your OS, it may not be version 3.0 or later.

Console - user@hostname ~ $

1
2
3
4
5
6
7
8
mkdir -p ~/Downloads
cd ~/Downloads
curl -O http://ftp.gnu.org/gnu/bison/bison-3.0.4.tar.gz
tar -xzf bison-3.0.4.tar.gz
cd bison-3.0.4
./configure
make
sudo make install

RVM

In this article, we’ll use RVM for getting ruby. If you haven’t installed rvm, it can be done like so

Console - user@hostname ~ $

1
curl -L https://get.rvm.io | bash -s stable --ruby=1.9.3

You may need to run the following to the rvm to install:

Console - user@hostname ~ $

1
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

Once rvm is installed, you’ll have to run the following for the current shell to see it:

Console - user@hostname ~ $

1
source $HOME/.rvm/scripts/rvm

If you already have rvm installed but not ruby 1.9.3

Console - user@hostname ~ $

1
rvm install ruby-1.9.3

Oniguruma

Console - user@hostname ~ $

1
2
3
4
5
6
7
8
mkdir -p ~/Downloads
cd ~/Downloads
curl -O http://www.geocities.jp/kosako3/oniguruma/archive/onig-5.9.6.tar.gz
tar -xzf onig-5.9.6.tar.gz
cd onig-5.9.6
./configure
make
sudo make install

May need to refresh the library cache

Console - user@hostname ~ $

1
sudo ldconfig -v

JQ

Ruby Virtual Environment

Console - user@hostname ~ $

1
rvm ruby-1.9.3@jq --create

Checkout

Console - user@hostname ~ $

1
2
3
mkdir -p ~/Downloads
cd ~/Downloads
git clone https://github.com/stedolan/jq.git

Choose Version

You can skip this if you want to build the absolute latest.

The following will show you what versions are available:

Console - user@hostname ~ $

1
2
cd ~/Download/jq
git tag

You can choose a version (1.4 in this case) like so:

Console - user@hostname ~ $

1
2
cd ~/Download/jq
git checkout jq-1.4

Install Dependencies

Console - user@hostname ~ $

1
2
cd ~/Downloads/jq/docs
bundle install

Build

Console - user@hostname ~ $

1
2
3
4
5
cd ~/Downloads/jq
autoreconf -i
./configure
make
sudo make install

JQ 1.2 and Earlier

Installation

Console - user@hostname ~ $

1
2
git clone https://github.com/stedolan/jq.git
cd jq

If you have a later version of the jq repository but want to build the older version, you can get it like so:

Console - user@hostname ~ $

1
git checkout jq-1.2

Run the following to build:

Console - user@hostname ~ $

1
make && sudo make install

Requires:

  • flex
  • bison
  • python
  • gcc
  • make

Note

There is also a soft dependency for Bonsai. The first time we run sudo make install it will try to build the documentation as well, if bonsai is not installed the make will fail with a message that looks something like this:

Failed make install

1
2
3
4
5
6
( cd docs; rake manpage; ) > jq.1
rake aborted!
no such file to load -- bonsai

(See full trace by running task with --trace)
make: *** [jq.1] Error 1

Just run sudo make install again and it should skip building the docs and finish the install.

References

Comments