LoginSignup
7
3

More than 5 years have passed since last update.

コマンドラインでcURLでAPIを呼んでJSONが帰ってくるときにデコードして出力

Last updated at Posted at 2016-10-30

はじめに

ちょっとAPIのテストしたいなーって時に戻り値がJSONで、「どんな風に辿れば良いのかな」とか「欲しいデータあるかな」と思ったので試しました。Macです。

データ

今回の対象はライブドアの天気予報のAPIです。便利ですね。

$ curl 'http://weather.livedoor.com/forecast/webservice/json/v1?city=070030'

{"pinpointLocations":[{"link":"http://weather.livedoor.com ...

Screen Shot 2016-10-30 at 20.45.38.png

Livedoor お天気Webサービス仕様
http://weather.livedoor.com/weather_hacks/webservice

PHP

普段はPHPを使っているのでPHPでのデコードです。brewでCLI版のPHPを入れてしまっていますが、確かデフォルトでも動きます。

 curl 'http://weather.livedoor.com/forecast/webservice/json/v1?city=070030' | php -r 'var_dump(json_decode(trim(fgets(STDIN))));'

Screen Shot 2016-10-30 at 20.36.54.png

呼び出しがこんがらがりそうです。
var_dump(), json_decode(), trim(), fgets(), STDINですね。

jq

./jqというJSON processorを使いました。

https://stedolan.github.io/jq/

curl 'http://weather.livedoor.com/forecast/webservice/json/v1?city=070030' | jq

Screen Shot 2016-10-30 at 20.42.19.png

使い勝手も簡単ですし、ターミナルによりますがデフォルトで色づけされます。便利ですね。

ですが初回はインストールする必要があります。

brew install jq

まとめ

jqはお手軽で便利だなぁ、と。他の言語でもきっと出来るとおもいます。

7
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3