これはMacのターミナルでcurlコマンドを使用していた時の話。
色々あって、https://pokeapi.co/api/v2/move/47からデータを取得していた。
取得したデータは下記。
$ curl https://pokeapi.co/api/v2/move/47
{"accuracy":55,"contest_combos":{"normal":{"use_after":null,"use_before":[{"name":"perish-song","url":"https://pokeapi.co/api/v2/move/195/"},{"name":"refresh","url":"https://pokeapi.co/api/v2/move/287/"}]},"super":{"use_after":null,"use_before":null}},"contest_effect":{"url":"https://pokeapi.co/api/v2/contest-effect/18/"},"contest_type":{"name":"cute","url":"https://pokeapi.co/api/v2/contest-type/3/"},"damage_class":{"name":"status","url":"https://pokeapi.co/api/v2/move-damage-class/1/"},"effect_chance":null,"effect_changes":[],"effect_entries":[{"effect":"Puts the target to sleep.","language":{"name":"en","url":"https://pokeapi.co/api/v2/language/9/"},"sho
~~以下略
このままでは人の目ではとても見辛いが、curlで取ってきたデータをjqコマンドに渡してJSONでEncodeすると見やすくなるとの事を小耳に挟む。
それは便利だ、と早速やってみた。
curl “URL” の後に”|”パイプで渡してjqコマンドで処理
curl https://pokeapi.co/api/v2/move/47 | jq
で良いはず。
$ curl https://pokeapi.co/api/v2/move/47 | jq
zsh: command not found: jq.
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 16519 0 16519 0 0 199k 0 --:--:-- --:--:-- --:--:-- 199k
curl: (23) Failed writing body (0 != 944)
しかし、見ての通り、どうも上手く行かない。
curlの処理自体は100%終わっている様に見受けられるので、データダウンロードは上手く行っている様だ。
command not found: jq.って事は、インストールが必要なのかも?
と言う事でインストールからやってみた。
Macユーザーは、Homebrewでjqをインストールできます。
$ brew install jq Updating Homebrew... ==> Downloading https://ghcr.io/v2/homebrew/core/oniguruma/manifests/6.9.7.1 ######################################################################## 100.0% ==> Downloading 〜以下略
インストール完了。
M1チップの最近のMacでもHomebrewは対応した様ですので、今後もMacユーザーはできる限りHomebrewでダウンロードするのが吉と考えます。
と言う事で、即実行。
curlで指定URLからGETしたデータをパイプでjqコマンドに渡すコマンドが” curl https://pokeapi.co/api/v2/move/47 | jq “
$ curl https://pokeapi.co/api/v2/move/47 | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 16519 0 16519 0 0 97170 0 --:--:-- --:--:-- --:--:-- 97170
{
"accuracy": 55,
"contest_combos": {
"normal": {
"use_after": null,
"use_before": [
{
"name": "perish-song",
"url": "https://pokeapi.co/api/v2/move/195/"
},
{
"name": "refresh",
"url": "https://pokeapi.co/api/v2/move/287/"
}
]
},
"super": {
"use_after": null,
"use_before": null
}
},
"contest_effect": {
"url": "https://pokeapi.co/api/v2/contest-effect/18/"
},
"contest_type": {
"name": "cute",
"url": "https://pokeapi.co/api/v2/contest-type/3/"
},
"damage_class": {
"name": "status",
"url": "https://pokeapi.co/api/v2/move-damage-class/1/"
},
"effect_chance": null,
"effect_changes": [],
"effect_entries": [
{
"effect": "Puts the target to sleep.",
"language": {
"name": "en",
"url": "https://pokeapi.co/api/v2/language/9/"
},
"short_effect": "Puts the target to sleep."
}
〜以下略
これでJSONにEncodeされた人の目で見やすいデータが出てきました。
「目で確認出来る」事以上に、この形式で出力できる、と言う事はそのデータはJSONの形式に合致していると言う事も分かり、当該データを安心して使用する事が出来ます。
そんな便利なjqコマンドを知った備忘録として。


コメント