LoginSignup
2
2

More than 1 year has passed since last update.

jq入門

Last updated at Posted at 2021-08-06

jqとは?

コマンドラインでJSONを処理するツールです。curlと組み合わせると色々できます。

インストール

macOS

brew install jq

Ubuntu

apt update
apt install jq

Dockerfile

Visual Studio CodeのDev Containerとかで使う用です。

RUN apt-get update && apt-get install -y \
  jq \
  && rm -rf /var/lib/apt/lists/*

Windows

Chocolatey NuGet入れてから、

chocolatey install jq

あるいはバイナリをダウンロードしてPATH以下に設置します。

多分Dev Container使った方がいいです。

使い方

手作業用

curl -fsSL 'https://httpbin.org/get?a=1&a=2&a=3' | jq

jq-manual.png

整形されて色がつきます。手作業ならこれで十分です。

バッチ用

オプションに-rを加えます。とりあえずおまじないということにしておいて良いです。
これをつけると普通のシェルコマンドとの互換性がでます。forでループ回したりできます。

curl -fsSL 'https://httpbin.org/get?a=1&a=2&a=3' | jq -r '.args.a[]'
1
2
3

もちろん普通に配列の要素を取り出すこともできます。

curl -fsSL 'https://httpbin.org/get?a=1&a=2&a=3' | jq -r '.args.a[1]'
2

パターンの先頭にルートを示すドットが必要なことに注意が必要です。

結論

これ以上のことが必要な人は別の記事を参照してください。jq初心者卒業おめでとうございます。

2
2
0

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
2
2