「jq」の版間の差分

提供: Node.js/JavaScript入門
移動: 案内検索
 
(同じ利用者による、間の1版が非表示)
行116: 行116:
 
"bar is 20"
 
"bar is 20"
 
"hoge is 30"
 
"hoge is 30"
 +
$ jq -r '.[]| "\(.name) is \(.age)"' b.json
 +
foo is 10
 +
bar is 20
 +
hoge is 30
 +
</syntaxhighlight>
 +
== 配列の中身を取り出す ==
 +
=== dns.json ===
 +
以下の dns.json というファイルがあったとします。
 +
<syntaxhighlight lang="javascript">
 +
[
 +
        { "dns_names":["www.foo.com"] },
 +
        { "dns_names":["login.foo.com"] },
 +
        { "dns_names":["a.foo.com", "b.foo.com", "c.foo.com"] }
 +
]
 +
</syntaxhighlight>
 +
=== 一番上の配列を取り外す ===
 +
<syntaxhighlight lang="bash">
 +
$ jq '.[]' dns.json
 +
{
 +
  "dns_names": [
 +
    "www.foo.com"
 +
  ]
 +
}
 +
{
 +
  "dns_names": [
 +
    "login.foo.com"
 +
  ]
 +
}
 +
{
 +
  "dns_names": [
 +
    "a.foo.com",
 +
    "b.foo.com",
 +
    "c.foo.com"
 +
  ]
 +
}
 +
</syntaxhighlight>
 +
=== キーを指定して取り出す ===
 +
まだ、配列として、取り出されています。
 +
<syntaxhighlight lang="bash">
 +
$ jq '.[]|.["dns_names"]' dns.json
 +
[
 +
  "www.foo.com"
 +
]
 +
[
 +
  "login.foo.com"
 +
]
 +
[
 +
  "a.foo.com",
 +
  "b.foo.com",
 +
  "c.foo.com"
 +
]
 +
</syntaxhighlight>
 +
=== さらにかっこを外す ===
 +
<syntaxhighlight lang="bash">
 +
$ jq '.[]|.["dns_names"]|.[]' dns.json
 +
"www.foo.com"
 +
"login.foo.com"
 +
"a.foo.com"
 +
"b.foo.com"
 +
"c.foo.com"
 +
</syntaxhighlight>
 +
=== ダブルクォートを外す ===
 +
-r オプションを使えば、ダブルクォートを外せます。
 +
<syntaxhighlight lang="bash">
 +
$ jq -r '.[]|.["dns_names"]|.[]' dns.json
 +
www.foo.com
 +
login.foo.com
 +
a.foo.com
 +
b.foo.com
 +
c.foo.com
 +
</syntaxhighlight>
 +
 +
== 2つのキーを取り出す ==
 +
=== dns.2json ===
 +
<syntaxhighlight lang="javascript">
 +
[
 +
        { "dns_names":["www.foo.com"], "issuer":"EV" },
 +
        { "dns_names":["login.foo.com"], "issuer":"EV" },
 +
        { "dns_names":["a.foo.com", "b.foo.com", "c.foo.com"], "issuer":"OV" }
 +
]
 +
</syntaxhighlight>
 +
===  2つのキーを取り出してみる ===
 +
dns_names と issuer を取り出してみます。
 +
"," (カンマ) を使うことで、複数の値を取り出すことが指定できます。
 +
<syntaxhighlight lang="bash">
 +
$ jq -r '.[]|."dns_names",  ."issuer"' dns2.json
 +
[
 +
  "www.foo.com"
 +
]
 +
EV
 +
[
 +
  "login.foo.com"
 +
]
 +
EV
 +
[
 +
  "a.foo.com",
 +
  "b.foo.com",
 +
  "c.foo.com"
 +
]
 +
OV
 +
</syntaxhighlight>
 +
=== さらに配列のかぎかっこを外す ===
 +
さらに鍵かっこを dns_names の後ろに指定すると、かっこが外せます。ただ、このデータのままで使いやすいのか、という問題は残ってます。
 +
<syntaxhighlight lang="bash">
 +
$ jq -r '.[]|."dns_names"[],  ."issuer"' dns2.json
 +
www.foo.com
 +
EV
 +
login.foo.com
 +
EV
 +
a.foo.com
 +
b.foo.com
 +
c.foo.com
 +
OV
 +
</syntaxhighlight>
 +
===  成形してみる ===
 +
これだと、何も変わってない。
 +
<syntaxhighlight lang="bash">
 +
$ jq -r '.[]| {dns_names: ."dns_names",  issuer} ' dns2.json
 +
{
 +
  "dns_names": [
 +
    "www.foo.com"
 +
  ],
 +
  "issuer": "EV"
 +
}
 +
{
 +
  "dns_names": [
 +
    "login.foo.com"
 +
  ],
 +
  "issuer": "EV"
 +
}
 +
{
 +
  "dns_names": [
 +
    "a.foo.com",
 +
    "b.foo.com",
 +
    "c.foo.com"
 +
  ],
 +
  "issuer": "OV"
 +
}
 +
</syntaxhighlight>
 +
=== 配列を展開してみる ===
 +
dns_names の値は配列になっていて、1つ以上のFQDNが入っています。
 +
エントリごとに、 dns_names と issuer があります。
 +
dns_names の値は配列ですが、その1つ1つに対して、 issuer を入れる場合は、以下のように書くと、 a.foo.com, b.foo.com, c.foo.com が別々に展開されます。
 +
<syntaxhighlight lang="bash">
 +
$ jq -r '.[]| {dns_names: ."dns_names"[],  issuer} ' dns2.json
 +
{
 +
  "dns_names": "www.foo.com",
 +
  "issuer": "EV"
 +
}
 +
{
 +
  "dns_names": "login.foo.com",
 +
  "issuer": "EV"
 +
}
 +
{
 +
  "dns_names": "a.foo.com",
 +
  "issuer": "OV"
 +
}
 +
{
 +
  "dns_names": "b.foo.com",
 +
  "issuer": "OV"
 +
}
 +
{
 +
  "dns_names": "c.foo.com",
 +
  "issuer": "OV"
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
== 関連項目 ==
 
== 関連項目 ==

2018年3月21日 (水) 18:36時点における最新版

jqコマンドとは、コマンドライン JSON プロセッサです。jsonデータの整形や抽出ができます。jqコマンドとは別の存在として、node.jsnpmモジュールとして、サーバサイド jQuery ラッパーの名前も jq です。

読み方

jq
じぇーきゅー

概要

jqコマンドは、awkやsedのように整形や加工を行うコマンドです。

直列で表示されたJSONフォーマットは、ヒューマンリーダブルではありません。

[{"name":"foo","age":10},{"name":"bar","age":20},{"name":"hoge","age":30}]

このように展開することによって、可読性が改善します。

[
  {
    "age": 10,
    "name": "foo"
  },
  {
    "age": 20,
    "name": "bar"
  },
  {
    "age": 30,
    "name": "hoge"
  }
]

インストール

FreeBSD

$ sudo pkg install jq

Ubuntu

$ sudo apt-get install jq

CentOS

$ sudo yum -y install jq

Mac

brew install jq

使い方

コマンドラインオプション

jq - commandline JSON processor [version 1.3]
Usage: jq [options] <jq filter> [file...]
 
For a description of the command line options and
how to write jq filters (and why you might want to)
see the jq manpage, or the online documentation at
http://stedolan.github.com/jq

JSONを整形して表示する

$ cat b.json
[{"name":"foo","age":10},{"name":"bar","age":20},{"name":"hoge","age":30}]
$ jq . b.json
[
  {
    "age": 10,
    "name": "foo"
  },
  {
    "age": 20,
    "name": "bar"
  },
  {
    "age": 30,
    "name": "hoge"
  }
]

JSONからn番目のエントリを抽出する

インデックスの値は、0から数えます。2番目を表示するには、1を指定します。

$ jq '.[1]' b.json
{
  "age": 20,
  "name": "bar"
}

JSONで特定のフィールドを抽出する

2番めのageのフィールドだけを抽出します。

$ jq '.[1].age' b.json
20

すべてのageのフィールドを抽出します。

$ jq '.[].age' b.json
10
20
30

値の範囲を指定する

$ jq '.[].age >= 20 ' b.json
false
true
true

必要なデータを取り出して、整形する

$ jq '.[]| "\(.name) is \(.age)"' b.json
"foo is 10"
"bar is 20"
"hoge is 30"
$ jq -r '.[]| "\(.name) is \(.age)"' b.json
foo is 10
bar is 20
hoge is 30

配列の中身を取り出す

dns.json

以下の dns.json というファイルがあったとします。

[
        { "dns_names":["www.foo.com"] },
        { "dns_names":["login.foo.com"] },
        { "dns_names":["a.foo.com", "b.foo.com", "c.foo.com"] }
]

一番上の配列を取り外す

$ jq '.[]' dns.json
{
  "dns_names": [
    "www.foo.com"
  ]
}
{
  "dns_names": [
    "login.foo.com"
  ]
}
{
  "dns_names": [
    "a.foo.com",
    "b.foo.com",
    "c.foo.com"
  ]
}

キーを指定して取り出す

まだ、配列として、取り出されています。

$ jq '.[]|.["dns_names"]' dns.json
[
  "www.foo.com"
]
[
  "login.foo.com"
]
[
  "a.foo.com",
  "b.foo.com",
  "c.foo.com"
]

さらにかっこを外す

$ jq '.[]|.["dns_names"]|.[]' dns.json
"www.foo.com"
"login.foo.com"
"a.foo.com"
"b.foo.com"
"c.foo.com"

ダブルクォートを外す

-r オプションを使えば、ダブルクォートを外せます。

$ jq -r '.[]|.["dns_names"]|.[]' dns.json
www.foo.com
login.foo.com
a.foo.com
b.foo.com
c.foo.com

2つのキーを取り出す

dns.2json

[
        { "dns_names":["www.foo.com"], "issuer":"EV" },
        { "dns_names":["login.foo.com"], "issuer":"EV" },
        { "dns_names":["a.foo.com", "b.foo.com", "c.foo.com"], "issuer":"OV" }
]

2つのキーを取り出してみる

dns_names と issuer を取り出してみます。 "," (カンマ) を使うことで、複数の値を取り出すことが指定できます。

$ jq -r '.[]|."dns_names",  ."issuer"' dns2.json
[
  "www.foo.com"
]
EV
[
  "login.foo.com"
]
EV
[
  "a.foo.com",
  "b.foo.com",
  "c.foo.com"
]
OV

さらに配列のかぎかっこを外す

さらに鍵かっこを dns_names の後ろに指定すると、かっこが外せます。ただ、このデータのままで使いやすいのか、という問題は残ってます。

$ jq -r '.[]|."dns_names"[],  ."issuer"' dns2.json
www.foo.com
EV
login.foo.com
EV
a.foo.com
b.foo.com
c.foo.com
OV

成形してみる

これだと、何も変わってない。

$ jq -r '.[]| {dns_names: ."dns_names",  issuer} ' dns2.json
{
  "dns_names": [
    "www.foo.com"
  ],
  "issuer": "EV"
}
{
  "dns_names": [
    "login.foo.com"
  ],
  "issuer": "EV"
}
{
  "dns_names": [
    "a.foo.com",
    "b.foo.com",
    "c.foo.com"
  ],
  "issuer": "OV"
}

配列を展開してみる

dns_names の値は配列になっていて、1つ以上のFQDNが入っています。 エントリごとに、 dns_names と issuer があります。 dns_names の値は配列ですが、その1つ1つに対して、 issuer を入れる場合は、以下のように書くと、 a.foo.com, b.foo.com, c.foo.com が別々に展開されます。

$ jq -r '.[]| {dns_names: ."dns_names"[],  issuer} ' dns2.json
{
  "dns_names": "www.foo.com",
  "issuer": "EV"
}
{
  "dns_names": "login.foo.com",
  "issuer": "EV"
}
{
  "dns_names": "a.foo.com",
  "issuer": "OV"
}
{
  "dns_names": "b.foo.com",
  "issuer": "OV"
}
{
  "dns_names": "c.foo.com",
  "issuer": "OV"
}

関連項目