Json整形コマンド jqを使う

はじめに

Jsonを整形できるコマンド、jqを使ってみる。

環境

Windows 10 Professional
WSL2 (Ubuntu 22.04 LTS)

jqをインストール

sudo apt-get install jq
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  golang-1.18 golang-1.18-doc golang-1.18-go golang-1.18-src golang-doc golang-go golang-src pkg-config
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libjq1 libonig5
The following NEW packages will be installed:
  jq libjq1 libonig5
0 upgraded, 3 newly installed, 0 to remove and 28 not upgraded.
Need to get 357 kB of archives.
After this operation, 1087 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libonig5 amd64 6.9.7.1-2build1 [172 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 libjq1 amd64 1.6-2.1ubuntu3 [133 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 jq amd64 1.6-2.1ubuntu3 [52.5 kB]
Fetched 357 kB in 1s (263 kB/s)
Selecting previously unselected package libonig5:amd64.
(Reading database ... 79091 files and directories currently installed.)
Preparing to unpack .../libonig5_6.9.7.1-2build1_amd64.deb ...
Unpacking libonig5:amd64 (6.9.7.1-2build1) ...
Selecting previously unselected package libjq1:amd64.
Preparing to unpack .../libjq1_1.6-2.1ubuntu3_amd64.deb ...
Unpacking libjq1:amd64 (1.6-2.1ubuntu3) ...
Selecting previously unselected package jq.
Preparing to unpack .../jq_1.6-2.1ubuntu3_amd64.deb ...
Unpacking jq (1.6-2.1ubuntu3) ...
Setting up libonig5:amd64 (6.9.7.1-2build1) ...
Setting up libjq1:amd64 (1.6-2.1ubuntu3) ...
Setting up jq (1.6-2.1ubuntu3) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
/sbin/ldconfig.real: /usr/lib/wsl/lib/libcuda.so.1 is not a symbolic link

jqの使い方

jq --help

jq - commandline JSON processor [version 1.6]

Usage:  jq [options] <jq filter> [file...]
        jq [options] --args <jq filter> [strings...]
        jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Example:

        $ echo '{"foo": 0}' | jq .
        {
                "foo": 0
        }

Some of the options include:
  -c               compact instead of pretty-printed output;
  -n               use `null` as the single input value;
  -e               set the exit status code based on the output;
  -s               read (slurp) all inputs into an array; apply filter to it;
  -r               output raw strings, not JSON texts;
  -R               read raw strings, not JSON texts;
  -C               colorize JSON;
  -M               monochrome (don't colorize JSON);
  -S               sort keys of objects on output;
  --tab            use tabs for indentation;
  --arg a v        set variable $a to value <v>;
  --argjson a v    set variable $a to JSON value <v>;
  --slurpfile a f  set variable $a to an array of JSON texts read from <f>;
  --rawfile a f    set variable $a to a string consisting of the contents of <f>;
  --args           remaining arguments are string arguments, not files;
  --jsonargs       remaining arguments are JSON arguments, not files;
  --               terminates argument processing;

Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].

See the manpage for more options.

例:

echo '{"foo": 0}' | jq .
{
  "foo": 0
}

実際に使ってみる。

endoflife.date が丁度良さそうなので、ここのAPIにリクエストを送ってレスポンスを jqで整形する。

整形しない場合

curl --request GET \
  --url https://endoflife.date/api/php.json \
  --header 'Accept: application/json'

[{"cycle":"8.2","support":"2024-12-08","eol":"2025-12-08","latest":"8.2.8","latestReleaseDate":"2023-07-06","releaseDate":"2022-12-08","lts":false},{"cycle":"8.1","support":"2023-11-25","eol":"2024-11-25","latest":"8.1.21","latestReleaseDate":"2023-07-06","releaseDate":"2021-11-25","lts":false},{"cycle":"8.0","support":"2022-11-26","eol":"2023-11-26","latest":"8.0.29","latestReleaseDate":"2023-06-08","releaseDate":"2020-11-26","lts":false},{"cycle":"7.4","support":"2021-11-28","eol":"2022-11-28","latest":"7.4.33","latestReleaseDate":"2022-11-03","releaseDate":"2019-11-28","lts":false},{"cycle":"7.3","support":"2020-12-06","eol":"2021-12-06","latest":"7.3.33","latestReleaseDate":"2021-11-18","releaseDate":"2018-12-06","lts":false},{"cycle":"7.2","support":"2019-11-30","eol":"2020-11-30","latest":"7.2.34","latestReleaseDate":"2020-10-01","releaseDate":"2017-11-30","lts":false},{"cycle":"7.1","support":"2018-12-01","eol":"2019-12-01","latest":"7.1.33","latestReleaseDate":"2019-10-24","releaseDate":"2016-12-01","lts":false},{"cycle":"7.0","support":"2018-01-04","eol":"2019-01-10","latest":"7.0.33","latestReleaseDate":"2019-01-10","releaseDate":"2015-12-03","lts":false},{"cycle":"5.6","support":"2017-01-19","eol":"2018-12-31","latest":"5.6.40","latestReleaseDate":"2019-01-10","releaseDate":"2014-08-28","lts":false},{"cycle":"5.5","support":"2015-07-10","eol":"2016-07-21","latest":"5.5.38","latestReleaseDate":"2016-07-21","releaseDate":"2013-06-20","lts":false},{"cycle":"5.4","support":"2014-09-14","eol":"2015-09-14","latest":"5.4.45","latestReleaseDate":"2015-09-03","releaseDate":"2012-03-01","lts":false},{"cycle":"5.3","support":"2011-06-30","eol":"2014-08-14","latest":"5.3.29","latestReleaseDate":"2014-08-14","releaseDate":"2009-06-30","lts":false},{"cycle":"5.2","support":"2008-11-02","eol":"2011-01-06","latest":"5.2.17","latestReleaseDate":"2011-01-06","releaseDate":"2006-11-02","lts":false},{"cycle":"5.1","support":"2006-08-24","eol":"2006-08-24","latest":"5.1.6","latestReleaseDate":"2006-08-24","releaseDate":"2005-11-24","lts":false},{"cycle":"5.0","support":"2005-09-05","eol":"2005-09-05","latest":"5.0.5","latestReleaseDate":"2005-09-05","releaseDate":"2004-07-13","lts":false}]

整形する場合

curl --request GET   --url https://endoflife.date/api/php.json   --header 'Accept: application/json' | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2219  100  2219    0     0   5939      0 --:--:-- --:--:-- --:--:--  5933
[
  {
    "cycle": "8.2",
    "support": "2024-12-08",
    "eol": "2025-12-08",
    "latest": "8.2.8",
    "latestReleaseDate": "2023-07-06",
    "releaseDate": "2022-12-08",
    "lts": false
  },
  {
    "cycle": "8.1",
    "support": "2023-11-25",
    "eol": "2024-11-25",
    "latest": "8.1.21",
    "latestReleaseDate": "2023-07-06",
    "releaseDate": "2021-11-25",
    "lts": false
  },
  {
    "cycle": "8.0",
    "support": "2022-11-26",
    "eol": "2023-11-26",
    "latest": "8.0.29",
    "latestReleaseDate": "2023-06-08",
    "releaseDate": "2020-11-26",
    "lts": false
  },
  {
    "cycle": "7.4",
    "support": "2021-11-28",
    "eol": "2022-11-28",
    "latest": "7.4.33",
    "latestReleaseDate": "2022-11-03",
    "releaseDate": "2019-11-28",
    "lts": false
  },
  {
    "cycle": "7.3",
    "support": "2020-12-06",
    "eol": "2021-12-06",
    "latest": "7.3.33",
    "latestReleaseDate": "2021-11-18",
    "releaseDate": "2018-12-06",
    "lts": false
  },
  {
    "cycle": "7.2",
    "support": "2019-11-30",
    "eol": "2020-11-30",
    "latest": "7.2.34",
    "latestReleaseDate": "2020-10-01",
    "releaseDate": "2017-11-30",
    "lts": false
  },
  {
    "cycle": "7.1",
    "support": "2018-12-01",
    "eol": "2019-12-01",
    "latest": "7.1.33",
    "latestReleaseDate": "2019-10-24",
    "releaseDate": "2016-12-01",
    "lts": false
  },
  {
    "cycle": "7.0",
    "support": "2018-01-04",
    "eol": "2019-01-10",
    "latest": "7.0.33",
    "latestReleaseDate": "2019-01-10",
    "releaseDate": "2015-12-03",
    "lts": false
  },
  {
    "cycle": "5.6",
    "support": "2017-01-19",
    "eol": "2018-12-31",
    "latest": "5.6.40",
    "latestReleaseDate": "2019-01-10",
    "releaseDate": "2014-08-28",
    "lts": false
  },
  {
    "cycle": "5.5",
    "support": "2015-07-10",
    "eol": "2016-07-21",
    "latest": "5.5.38",
    "latestReleaseDate": "2016-07-21",
    "releaseDate": "2013-06-20",
    "lts": false
  },
  {
    "cycle": "5.4",
    "support": "2014-09-14",
    "eol": "2015-09-14",
    "latest": "5.4.45",
    "latestReleaseDate": "2015-09-03",
    "releaseDate": "2012-03-01",
    "lts": false
  },
  {
    "cycle": "5.3",
    "support": "2011-06-30",
    "eol": "2014-08-14",
    "latest": "5.3.29",
    "latestReleaseDate": "2014-08-14",
    "releaseDate": "2009-06-30",
    "lts": false
  },
  {
    "cycle": "5.2",
    "support": "2008-11-02",
    "eol": "2011-01-06",
    "latest": "5.2.17",
    "latestReleaseDate": "2011-01-06",
    "releaseDate": "2006-11-02",
    "lts": false
  },
  {
    "cycle": "5.1",
    "support": "2006-08-24",
    "eol": "2006-08-24",
    "latest": "5.1.6",
    "latestReleaseDate": "2006-08-24",
    "releaseDate": "2005-11-24",
    "lts": false
  },
  {
    "cycle": "5.0",
    "support": "2005-09-05",
    "eol": "2005-09-05",
    "latest": "5.0.5",
    "latestReleaseDate": "2005-09-05",
    "releaseDate": "2004-07-13",
    "lts": false
  }
]

参考

おわりに

jq は存在は知っていたのだが、使用したことはなかったので使用してみた。
コマンドラインで色々するときは使いそうだなぁ~。

Hugo で構築されています。
テーマ StackJimmy によって設計されています。