LoginSignup
24
18

More than 5 years have passed since last update.

jqクソ良い

Last updated at Posted at 2013-08-09

jqいいわ

今更jqに触れた。

DigitalOceanのimage idを調べたいんだけど...

packerDigitalOcean builderを指定していたとき、image_idを指定したいのだけれど知りたいためにはAPIを叩かないといけない。
叩くのはいいんだけどJSONで結果が返ってくる。

pretty-printしない場合

curl -X GET "https://api.digitalocean.com/images/?client_id={ユーザーのclient_id}&api_key={ユーザーのapi_key}

スクリーンショット 2013-08-08 0.02.26.png

ウッつらい…これ人間が読むものじゃないよな...

pretty-printしたい場合

jqコマンドに対して、APIのレスポンスをパイプでつないでやると...

curl -X GET "https://api.digitalocean.com/images/?client_id={ユーザーのclient_id}&api_key={ユーザーのapi_key} | jq '.'

スクリーンショット 2013-08-08 0.03.51.png

美しい...綺麗で見やすい。

これだけじゃアレなんで

パイプで結果をつないでいけるので簡単なの試してみた。

curl -X GET "https://api.digitalocean.com/images/?client_id={ユーザーのclient_id}&api_key={ユーザーのapi_key} " | jq '.images | .[] | if .distribution == "Ubuntu" then .distribution, .name, .id else "" end'
  1. 出力されたJSONをフィルタリング
  2. .imagesの中身を取り出す
  3. arrayに入ってるので展開
  4. distribution key が "Ubuntu"であれば、distribution, name, id それぞれの中身を出す。なければ空文字("")を表示する。

結果

スクリーンショット 2013-08-10 0.25.28.png

うむむ

and, or, not, == など、評価結果についてBoolean(true or false)を返すのがアレで、
真だったらtrueじゃなく評価した値を返してくれるようにすると
多段フィルタリング可能なんじゃないかなーとか思ったりする。
if文がelse節必須なのもだるさが...

言語向けのバインディング

Python向けのjq.pyを見つけた。
が、以下の理由でインストール試してない...

環境はMac OS X 10.8.4。

  • setup.pyで指定しているファイル名にtypoがある (PRしといた)
  • (setup.pyを修正した上で)Python 3.3.2でインストールできない
  • (setup.pyを修正した上で)Python 2.7.5でインストールできない

うーむ。

Haskellバージョンが有る...

ブランチ見てたらhaskell-versionとかある。
実装のためのプロトタイプはHaskellで組んだのかなこれ...

総評

jqいいですね。

24
18
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
18