LoginSignup
2
2

More than 5 years have passed since last update.

CLIでjsonをエンコードしてくれるjqコマンド

Posted at

内容

  1. curlコマンドでAPI叩いてもレスポンスが文字化けしてつらい
  2. jqコマンドっていう便利なツールがあるらしい

課題 : ターミナルでcurlコマンドでAPI叩いた時にレスポンスが文字化けする


$ curl http://weather.livedoor.com/forecast/webservice/json/v1\?city=130010 | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  8249    0  8249    0     0   207k      0 --:--:-- --:--:-- --:--:--  211k
{
    "pinpointLocations": [
        {
            "link": "http://weather.livedoor.com/area/forecast/1310100",
            "name": "\u5343\u4ee3\u7530\u533a"
        },
        {
            "link": "http://weather.livedoor.com/area/forecast/1310200",
            "name": "\u4e2d\u592e\u533a"
        },
        {
            "link": "http://weather.livedoor.com/area/forecast/1310300",
            "name": "\u6e2f\u533a"
        },
        {
            "link": "http://weather.livedoor.com/area/forecast/1310400",
            "name": "\u65b0\u5bbf\u533a"
        },
        {
            "link": "http://weather.livedoor.com/area/forecast/1310500",
            "name": "\u6587\u4eac\u533a"
        },
        {
            "link": "http://weather.livedoor.com/area/forecast/1310600",
            "name": "\u53f0\u6771\u533a"
        },
# 略

ぐぬぬ... :thinking:

jqというツールを使う


$ brew install jq
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
kobalt                                                                                                                               makeself

==> Installing dependencies for jq: oniguruma
==> Installing jq dependency: oniguruma
==> Downloading https://homebrew.bintray.com/bottles/oniguruma-6.1.3.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring oniguruma-6.1.3.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/oniguruma/6.1.3: 17 files, 1.3M
==> Installing jq
==> Downloading https://homebrew.bintray.com/bottles/jq-1.5_2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring jq-1.5_2.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/jq/1.5_2: 18 files, 957.9K
➜  ~ which jq
/usr/local/bin/jq

もう一回叩く


curl http://weather.livedoor.com/forecast/webservice/json/v1\?city=130010 | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  8249    0  8249    0     0   184k      0 --:--:-- --:--:-- --:--:--  187k
{
  "pinpointLocations": [
    {
      "link": "http://weather.livedoor.com/area/forecast/1310100",
      "name": "千代田区"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1310200",
      "name": "中央区"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1310300",
      "name": "港区"
    },
    {
      "link": "http://weather.livedoor.com/area/forecast/1310400",
      "name": "新宿区"
    }
# 略

うん、大丈夫そう。

2
2
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
2
2