何も考えずにjsonを取ってくると、こうなる。
$ curl http://api.openweathermap.org/data/2.5/weather\?q\=Tokyo,jp {"coord":{"lon":139.69,"lat":35.69},"sys":{"type":1,"id":7619,"message":0.0358,"country":"JP","sunrise":1413146764,"sunset":1413187731},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],"base":"cmc stations","main":{"temp":294.22,"pressure":989,"humidity":94,"temp_min":293.15,"temp_max":295.15},"wind":{"speed":3.1,"deg":190,"var_beg":90,"var_end":230},"clouds":{"all":75},"dt":1413237480,"id":1850147,"name":"Tokyo","cod":200}
目視で確認する時に非常に見にくい。
json_ppを使う
$ curl http://api.openweathermap.org/data/2.5/weather\?q\=Tokyo,jp | json_pp { "base" : "cmc stations", "sys" : { "country" : "JP", "sunrise" : 1413146764, "sunset" : 1413187731, "id" : 7619, "type" : 1, "message" : 0.0358 }, "dt" : 1413237480, "name" : "Tokyo", "cod" : 200, "main" : { "pressure" : 989, "temp" : 294.22, "temp_min" : 293.15, "temp_max" : 295.15, "humidity" : 94 }, "wind" : { "var_end" : 230, "var_beg" : 90, "speed" : 3.1, "deg" : 190 }, "weather" : [ { "icon" : "04n", "id" : 803, "description" : "broken clouds", "main" : "Clouds" } ], "coord" : { "lat" : 35.69, "lon" : 139.69 }, "clouds" : { "all" : 75 }, "id" : 1850147 }
見やすくなった!
python -mjson.tool
を使う
$ curl http://api.openweathermap.org/data/2.5/weather\?q\=Tokyo,jp | python -mjson.tool { "base": "cmc stations", "clouds": { "all": 75 }, "cod": 200, "coord": { "lat": 35.69, "lon": 139.69 }, "dt": 1413237480, "id": 1850147, "main": { "humidity": 94, "pressure": 989, "temp": 294.22, "temp_max": 295.15, "temp_min": 293.15 }, "name": "Tokyo", "sys": { "country": "JP", "id": 7619, "message": 0.0358, "sunrise": 1413146764, "sunset": 1413187731, "type": 1 }, "weather": [ { "description": "broken clouds", "icon": "04n", "id": 803, "main": "Clouds" } ], "wind": { "deg": 190, "speed": 3.1, "var_beg": 90, "var_end": 230 } }
こちらも整形出来た!!json_ppと順番が違うのなんでだろう。
httpieを使う
$ http -b api.openweathermap.org/data/2.5/weather\?q\=Tokyo,jp
curlとちょっと書き方が違うけど、自動で色付けしてくれてありがたい。
まとめ
httpieを軽く使いつつ、curlとかその辺を使っていこう。
追記
CLIでJSONの整形をする - ( ꒪⌓꒪) ゆるよろ日記
こちらの記事の最初の方しか読んでなかったけど、一番下の方にjqというものが紹介されていた。
$ brew install jq
で入る。
$ curl http://api.openweathermap.org/data/2.5/weather\?q\=Tokyo,jp | jq '.' -C
いろいろあって便利そう。