Content-Length: 78201 | pFad | http://momijiame.tumblr.com/post/51299899637/jq
esJSON の内容を sed や awk のようにフィルタ・加工するためのプログラムに jq がある。
JSON 形式で提供されている WebAPI の出力や、ログの内容を扱うのにとても便利。
その強力さの一端を紹介したい。
インストールは Mac OS X であれば Homebrew から。公式サイトからバイナリをダウンロードすることもできる。
$ brew install jq
$ cat << EOS > jsonfile {"name": "Foo", "sex": "Male", "age": 15, "emails": []} {"name": "Bar", "sex": "Male", "age": 20, "emails": ["hoge@example.jp"]} {"name": "Baz", "sex": "Female", "emails": ["fuga@example.net", "piyo@example.org"]} EOS $ cat jsonfile {"name": "Foo", "sex": "Male", "age": 15, "emails": []} {"name": "Bar", "sex": "Male", "age": 20, "emails": ["hoge@example.jp"]} {"name": "Baz", "sex": "Female", "emails": ["fuga@example.net", "piyo@example.org"]}
$ cat jsonfile | jq '.' { "emails": [], "age": 15, "sex": "Male", "name": "Foo" } { "emails": [ "hoge@example.jp" ], "age": 20, "sex": "Male", "name": "Bar" } { "emails": [ "fuga@example.net", "piyo@example.org" ], "sex": "Female", "name": "Baz" }
$ cat jsonfile | jq '.name' "Foo" "Bar" "Baz" $ cat jsonfile | jq '.["name"]' "Foo" "Bar" "Baz"
$ cat jsonfile | jq '.emails' [] [ "hoge@example.jp" ] [ "fuga@example.net", "piyo@example.org" ] $ cat jsonfile | jq '.emails[]' "hoge@example.jp" "fuga@example.net" "piyo@example.org"
$ cat jsonfile | jq '.name, .age' "Foo" 15 "Bar" 20 "Baz" null $ cat jsonfile | jq '{name, age}' { "age": 15, "name": "Foo" } { "age": 20, "name": "Bar" } { "age": null, "name": "Baz" } $ cat jsonfile | jq '[.name, .age]' [ "Foo", 15 ] [ "Bar", 20 ] [ "Baz", null ]
$ cat jsonfile | jq '{"namae": .name}' { "namae": "Foo" } { "namae": "Bar" } { "namae": "Baz" } $ cat jsonfile | jq '{(.name): .emails}' { "Foo": [] } { "Bar": [ "hoge@example.jp" ] } { "Baz": [ "fuga@example.net", "piyo@example.org" ] }
$ cat jsonfile | jq '{name, "age": (.age + 5)}' { "age": 20, "name": "Foo" } { "age": 25, "name": "Bar" } { "age": 5, "name": "Baz" }
$ cat jsonfile | jq '.emails[]' "hoge@example.jp" "fuga@example.net" "piyo@example.org" $ cat jsonfile | jq '.emails[] | length' 15 16 16
$ cat jsonfile | jq 'keys' [ "age", "emails", "name", "sex" ] [ "age", "emails", "name", "sex" ] [ "emails", "name", "sex" ]
$ cat jsonfile | jq 'select(.sex == "Male")' { "emails": [], "age": 15, "sex": "Male", "name": "Foo" } { "emails": [ "hoge@example.jp" ], "age": 20, "sex": "Male", "name": "Bar" } $ cat jsonfile | jq 'select(.age > 15)' { "emails": [ "hoge@example.jp" ], "age": 20, "sex": "Male", "name": "Bar" } $ cat jsonfile | jq 'select(.age < 20 and .age > 10)' { "emails": [], "age": 15, "sex": "Male", "name": "Foo" }
$ cat jsonfile | jq 'has("age")' true true false $ cat jsonfile | jq 'select(has("age"))' { "emails": [], "age": 15, "sex": "Male", "name": "Foo" } { "emails": [ "hoge@example.jp" ], "age": 20, "sex": "Male", "name": "Bar" }
$ cat jsonfile | jq 'select(contains({age:20}))' { "emails": [ "hoge@example.jp" ], "age": 20, "sex": "Male", "name": "Bar" }
$ cat jsonfile | jq 'if .age >= 20 then "adult" else "minor" end' "minor" "adult" "minor"
Fetched URL: http://momijiame.tumblr.com/post/51299899637/jq
Alternative Proxies: