先日、WindowsでJSONをCSV形式に変換する必要がありました。
その際にjqを利用したので、使い方を簡単にまとめておこうと思います。
jqはJSONデータの整形やJSONからのデータ抽出ができるコマンドラインです。
以上のURLからダウンロード、詳細を確認することができます。
Windowsでは、以下の手順でjqを利用することが可能です。
>jq --version jq-1.6
※手順2でリネームしていない場合はjq-win64 --version
を実行。
COVID-19 Japan 新型コロナウイルス対策ダッシュボードのデータを使って、JSONをCSVに変換してみようと思います。
今回扱うのは、以下のようなデータです。
>curl https://www.stopcovid19.jp/data/covid19japan-trend.json [{"name":"Hokkaido","ncurrentpatients":136,"dcurrentpatients":10},{"name":"Aomori","ncurrentpatients":1,"dcurrentpatients":1},{"name":"Iwate","ncurrentpatients":0,"dcurrentpatients":0},{"name":"Miyagi","ncurrentpatients":54,"dcurrentpatients":-8},{"name":"Akita","ncurrentpatients":2,"dcurrentpatients":0},{"name":"Yamagata","ncurrentpatients":2,"dcurrentpatients":0},{"name":"Fukushima","ncurrentpatients":42,"dcurrentpatients":-1},{"name":"Ibaraki","ncurrentpatients":34,"dcurrentpatients":4},{"name":"Tochigi","ncurrentpatients":57,"dcurrentpatients":-11},{"name":"Gunma","ncurrentpatients":85,"dcurrentpatients":0},{"name":"Saitama","ncurrentpatients":270,"dcurrentpatients":-10},{"name":"Chiba","ncurrentpatients":285,"dcurrentpatients":-2},{"name":"Tokyo","ncurrentpatients":2184,"dcurrentpatients":34},{"name":"Kanagawa","ncurrentpatients":530,"dcurrentpatients":-2},{"name":"Niigata","ncurrentpatients":15,"dcurrentpatients":-4},{"name":"Toyama","ncurrentpatients":5,"dcurrentpatients":0},{"name":"Ishikawa","ncurrentpatients":52,"dcurrentpatients":-1},{"name":"Fukui","ncurrentpatients":3,"dcurrentpatients":-1},{"name":"Yamanashi","ncurrentpatients":4,"dcurrentpatients":1},{"name":"Nagano","ncurrentpatients":5,"dcurrentpatients":-4},{"name":"Gifu","ncurrentpatients":28,"dcurrentpatients":4},{"name":"Shizuoka","ncurrentpatients":15,"dcurrentpatients":2},{"name":"Aichi","ncurrentpatients":357,"dcurrentpatients":14},{"name":"Mie","ncurrentpatients":77,"dcurrentpatients":1},{"name":"Shiga","ncurrentpatients":24,"dcurrentpatients":-2},{"name":"Kyoto","ncurrentpatients":60,"dcurrentpatients":-7},{"name":"Osaka","ncurrentpatients":546,"dcurrentpatients":-29},{"name":"Hyogo","ncurrentpatients":87,"dcurrentpatients":-5},{"name":"Nara","ncurrentpatients":17,"dcurrentpatients":3},{"name":"Wakayama","ncurrentpatients":5,"dcurrentpatients":1},{"name":"Tottori","ncurrentpatients":7,"dcurrentpatients":0},{"name":"Shimane","ncurrentpatients":3,"dcurrentpatients":1},{"name":"Okayama","ncurrentpatients":1,"dcurrentpatients":0},{"name":"Hiroshima","ncurrentpatients":47,"dcurrentpatients":10},{"name":"Yamaguchi","ncurrentpatients":6,"dcurrentpatients":0},{"name":"Tokushima","ncurrentpatients":12,"dcurrentpatients":-5},{"name":"Kagawa","ncurrentpatients":3,"dcurrentpatients":0},{"name":"Ehime","ncurrentpatients":0,"dcurrentpatients":0},{"name":"Kochi","ncurrentpatients":1,"dcurrentpatients":0},{"name":"Fukuoka","ncurrentpatients":95,"dcurrentpatients":-22},{"name":"Saga","ncurrentpatients":2,"dcurrentpatients":0},{"name":"Nagasaki","ncurrentpatients":2,"dcurrentpatients":0},{"name":"Kumamoto","ncurrentpatients":7,"dcurrentpatients":0},{"name":"Oita","ncurrentpatients":3,"dcurrentpatients":0},{"name":"Miyazaki","ncurrentpatients":1,"dcurrentpatients":-1},{"name":"Kagoshima","ncurrentpatients":29,"dcurrentpatients":12},{"name":"Okinawa","ncurrentpatients":163,"dcurrentpatients":10}]
これを見やすいように整形してみます。
>curl https://www.stopcovid19.jp/data/covid19japan-trend.json | jq ".[]" { "name": "Hokkaido", "ncurrentpatients": 136, "dcurrentpatients": 10 } { "name": "Aomori", "ncurrentpatients": 1, "dcurrentpatients": 1 } { "name": "Iwate", "ncurrentpatients": 0, "dcurrentpatients": 0 } ~~~省略~~~
CSVに変換するためには、必要な項目を抽出し、配列にする必要があります。
今回は項目が少ないので、全項目抽出します。
>curl https://www.stopcovid19.jp/data/covid19japan-trend.json | jq.exe -r ".[] | [.name, .ncurrentpatients, .dcurrentpatients]" [ "Hokkaido", 136, 10 ] [ "Aomori", 1, 1 ] [ "Iwate", 0, 0 ] ~~~省略~~~
最後に「-r」で文字列の引用符を除いて、CSVに変換します。
>curl https://www.stopcovid19.jp/data/covid19japan-trend.json | jq.exe -r ".[] | [.name, .ncurrentpatients, .dcurrentpatients] | @csv" "Hokkaido",136,10 "Aomori",1,1 "Iwate",0,0 ~~~省略~~~
一応、「-r」オプションを付けないとこんな感じになってしまいます。
>curl https://www.stopcovid19.jp/data/covid19japan-trend.json | jq.exe ".[] | [.name, .ncurrentpatients, .dcurrentpatients] | @csv" "\"Hokkaido\",136,10" "\"Aomori\",1,1" "\"Iwate\",0,0" ~~~省略~~~
今回行ったCSVへの変換以外のjqの使い方は以下のマニュアルで確認できます。
また、以下のサイトでは、jqをオンライン上で実行することができます。
フィルタリングを色々と試すときはお勧めです。