info
-
is a lightweight and flexible command line json processor
-
jq is like
sed
for json datause it to slice and filter and map and transform
-
written in portable c
has zero runtime dependencies
-
tutorial
-
use github json api
-
get last 5 commits from the jq repo
$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'
-
the simplest jq program is the expression
.
$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.'
-
extract just the first commit
$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0]'
-
leave out the
curl
command$ ... | jq '.[0] | {message: .commit.message, name: .commit.committer.name}' # the | operator in jq feeds output of one filter `.[0]` # into the input of another `{...}` # you can access nested attributes, such as `.commit.message`
-
get the rest of the commits
$ ... | jq '.[] | {message: .commit.message, name: .commit.committer.name}' # `.[]` returns each element of the array returned in the response, one at a time # which are all fed into `{message: .commit.message, name: .commit.committer.name}` # data in jq represented as streams of json values # streams are serialised by just separating json values with whitespace # this is a `cat` friendly format # join 2 json streams together and get a valid json stream
-
get output as a single array
$ ... | jq '[.[] | {message: .commit.message, name: .commit.committer.name}]'
-
pull out all of the
html_url
fields inside that array of parent commits# and make a simple list of strings # to get along with the `message` and `author` fields $ ... | jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
-