Mac に jq コマンドのインストール

Mac

Mac Book Air を購入して、約2年間が経ちました。
この2年間は、Virtual Box を使って、Windows を使っているそんな感じだったっす。

ところが、今年になってある決意をしました。
「ちゃんとMac」として使ってみようと。。w

んなわけで、Virtual Box はアンインストールして、Windows の環境と決別したのですが(別PCには、しっかりWindows 10 入ってますけど)、そうすると何をしていいのか途端にわからなくなりました。。

と、いうことで、ターミナルから curl を叩いて、Web API で遊んでみようかなとなんとなく思いまして、ひとまず、jqコマンドがあった方がよさげなので、その辺からやっていこうかと思います。

 


JQコマンド

JQって何か一言で表現すると、
「jq is a lightweight and flexible command-line JSON processor.」
ですね。

JQのチュートリアルなどは、下記をご覧ください。
https://stedolan.github.io/jq/


JQのインストール

下記のコマンドでインストールできます。

$ brew install jq

 

ただ、自分の環境では、Homebrew も入ってなかったので、先にそっちをインストールしました。

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

JQを使ったサンプル

curl と組み合わせて使うのが多いみたいです。

サンプルの実行

$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq

実行結果

$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed
100 17623  100 17623    0     0  10030      0  0:00:01  0:00:01 --:--:-- 10030
[
  {
    "sha": "1740fd036dc6ab464ef0937a0f3fc81618886837",
    "commit": {
       "author": {
        "name": "Nicolas Williams",
        "email": "nico@cryptonector.com",
        "date": "2016-01-18T20:58:58Z"
      },
      "committer": {
        "name": "Nicolas Williams",
        "email": "nico@cryptonector.com",
        "date": "2016-01-19T06:19:24Z"
      },
    :
    :

こんな感じです。


フィルターもかけられます。

$ curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'

実行結果

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                Dload  Upload   Total   Spent    Left  Speed
100 17623  100 17623    0     0   8218      0  0:00:02  0:00:02 --:--:--  8219
{
  "message": "Travis-CI build for OSX (fix #1083)",
  "name": "Nicolas Williams"
}

 

この辺がこなれてくるとRestなAPIを叩くのを楽になりそうだなー。

コメント

タイトルとURLをコピーしました