17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

curlで日本語を含むJSONレスポンスを見やすく整形する方法

Last updated at Posted at 2018-10-17

Macのターミナルでcurlを叩く際、返ってくるJSONレスポンスに日本語が含まれていると、文字列がエスケープされておりそのままでは読めないことがあります。

サクッとレスポンス確認したい時に不便なので、ターミナル上で日本語表示できるやり方をメモしておきます。

追記:jqコマンドを使用する方がよりシンプルだったため別記事書きました
jqコマンドでcurlのJSONレスポンスを見やすくする - Qiita

単純にcurl叩いた場合

livedoor天気情報APIを使って説明します。まずは単純にcurlを叩いてみます。

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

返ってくるJSONは整形されておらず、日本語もエスケープされた状態なのでターミナル上で見るとサッパリです。

{"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/1330500","name":"\u65e5\u306e\u51fa\u753a"},{"link":"http://weather.livedoor.com/area/forecast/1330700","name":"\u6a9c\u539f\u6751"},{"link":"http://weather.livedoor.com/area/forecast/1330800","name":"\u5965\u591a\u6469\u753a"}],"link":"http://weather.livedoor.com/area/forecast/130010","forecasts":[{"dateLabel":"\u4eca\u65e5","telop":"\u6674\u306e\u3061\u66c7","date":"2018-10-17","temperature":{"min":null,"max":{"celsius":"22","fahrenheit":"71.6"}},"image":{"width":50,"url":"http://weather.livedoor.com/img/icon/5.gif","title":"\u6674\u306e\u3061\u66c7","height":31}},{"dateLabel":"\u660e\u65e5","telop":"\u66c7\u306e\u3061\u96e8","date":"2018-10-18","temperature":{"min":{"celsius":"15","fahrenheit":"59.0"},"max":{"celsius":"18","fahrenheit":"64.4"}},"image":{"width":50,"url":"http://weather.livedoor.com/img/icon/13.gif","title":"\u66c7\u306e\u3061\u96e8","height":31}},{"dateLabel":"\u660e\u5f8c\u65e5","telop":"\u66c7\u6642\u3005\u96e8","date":"2018-10-19","temperature":{"min":null,"max":null},"image":{"width":50,"url":"http://weather.livedoor.com/img/icon/10.gif","title":"\u66c7\u6642\u3005\u96e8","height":31}}],"location":{"city":"\u6771\u4eac","area":"\u95a2\u6771","prefecture":"\u6771\u4eac\u90fd"},"publicTime":"2018-10-17T11:00:00\u002b0900","copyright":{"provider":[{"link":"http://tenki.jp/","name":"\u65e5\u672c\u6c17\u8c61\u5354\u4f1a"}],"link":"http://weather.livedoor.com/","title":"(C) LINE Corporation","image":{"width":118,"link":"http://weather.livedoor.com/","url":"http://weather.livedoor.com/img/cmn/livedoor.gif","title":"livedoor \u5929\u6c17\u60c5\u5831","height":26}},"title":"\u6771\u4eac\u90fd \u6771\u4eac \u306e\u5929\u6c17","description":{"text":" \u65e5\u672c\u306e\u6771\u306b\u306f\u4f4e\u6c17\u5727\u304c\u3042\u3063\u3066\u3001\u4f4e\u6c17\u5727\u304b\u3089\u306e\u3073\u308b\u524d\u7dda\u304c\u65e5\u672c\u306e\u5357\u6d77\u4e0a\u306b\u304b\n\u3051\u3066\u505c\u6ede\u3057\u3066\u3044\u307e\u3059\u3002\u307e\u305f\u3001\u6771\u65e5\u672c\u306f\u5f31\u3044\u6c17\u5727\u306e\u8c37\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002\n\n\u3010\u95a2\u6771\u7532\u4fe1\u5730\u65b9\u3011\n \u95a2\u6771\u7532\u4fe1\u5730\u65b9\u306f\u3001\u304a\u304a\u3080\u306d\u66c7\u308a\u3067\u96e8\u306e\u964d\u3063\u3066\u3044\u308b\u6240\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u5185\u9678\u3067\n\u306f\u6674\u308c\u3066\u3044\u308b\u6240\u3082\u3042\u308a\u307e\u3059\u3002\n\n 17\u65e5\u306f\u3001\u6c17\u5727\u306e\u8c37\u3084\u5317\u6771\u304b\u3089\u306e\u51b7\u305f\u3044\u7a7a\u6c17\u306e\u5f71\u97ff\u306b\u3088\u308a\u3001\u66c7\u308a\u3067\u96e8\u306e\u964d\n\u308b\u6240\u304c\u3042\u308b\u3067\u3057\u3087\u3046\u3002\u663c\u9803\u307e\u3067\u306f\u5185\u9678\u3092\u4e2d\u5fc3\u306b\u6674\u308c\u308b\u6240\u3082\u3042\u308b\u898b\u8fbc\u307f\u3067\u3059\u3002\n\n 18\u65e5\u306f\u3001\u6c17\u5727\u306e\u8c37\u3084\u5317\u6771\u304b\u3089\u306e\u6e7f\u3063\u305f\u7a7a\u6c17\u306e\u5f71\u97ff\u306b\u3088\u308a\u3001\u66c7\u308a\u3084\u96e8\u3068\u306a\n\u308b\u898b\u8fbc\u307f\u3067\u3059\u3002\n\n \u95a2\u6771\u8fd1\u6d77\u3067\u306f\u300117\u65e5\u304b\u308918\u65e5\u306b\u304b\u3051\u3066\u3001\u3046\u306d\u308a\u3092\u4f34\u3044\u6ce2\u304c\u9ad8\u3044\u3067\u3057\u3087\n\u3046\u3002\u8239\u8236\u306f\u9ad8\u6ce2\u306b\u6ce8\u610f\u3057\u3066\u304f\u3060\u3055\u3044\u3002\n\n\u3010\u6771\u4eac\u5730\u65b9\u3011\n 17\u65e5\u306f\u3001\u6674\u308c\u663c\u904e\u304e\u304b\u3089\u66c7\u308a\u3067\u3001\u6240\u306b\u3088\u308a\u5915\u65b9\u304b\u3089\u96e8\u3067\u3057\u3087\u3046\u3002\n 18\u65e5\u306f\u3001\u66c7\u308a\u3067\u3001\u591c\u306f\u96e8\u306e\u898b\u8fbc\u307f\u3067\u3059\u3002","publicTime":"2018-10-17T10:42:00\u002b0900"}}

ツラい。

Pythonで繋いで出力する

JSON整形するだけなら、パイプでpython -m json.toolを追加すれば良いのですが、これだと日本語はエスケープされたままなので微妙です。

そこで、python -c 'import sys,json;print json.dumps(json.loads(sys.stdin.read()),indent=4,ensure_ascii=False)'を繋げて叩いてみます。

$ curl http://weather.livedoor.com/forecast/webservice/json/v1?city=130010 | python -c 'import sys,json;print json.dumps(json.loads(sys.stdin.read()),indent=4,ensure_ascii=False)'

引数のensure_ascii=Falseを追加することで、日本語エスケープしなくなります。

これでJSONレスポンスが整形され、日本語も読めるようになりました。

{
    "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/1330500", 
            "name": "日の出町"
        }, 
        {
            "link": "http://weather.livedoor.com/area/forecast/1330700", 
            "name": "檜原村"
        }, 
        {
            "link": "http://weather.livedoor.com/area/forecast/1330800", 
            "name": "奥多摩町"
        }
    ], 
    "description": {
        "text": " 日本の東には低気圧があって、低気圧からのびる前線が日本の南海上にか\nけて停滞しています。また、東日本は弱い気圧の谷となっています。\n\n【関東甲信地方】\n 関東甲信地方は、おおむね曇りで雨の降っている所がありますが、内陸で\nは晴れている所もあります。\n\n 17日は、気圧の谷や北東からの冷たい空気の影響により、曇りで雨の降\nる所があるでしょう。昼頃までは内陸を中心に晴れる所もある見込みです。\n\n 18日は、気圧の谷や北東からの湿った空気の影響により、曇りや雨とな\nる見込みです。\n\n 関東近海では、17日から18日にかけて、うねりを伴い波が高いでしょ\nう。船舶は高波に注意してください。\n\n【東京地方】\n 17日は、晴れ昼過ぎから曇りで、所により夕方から雨でしょう。\n 18日は、曇りで、夜は雨の見込みです。", 
        "publicTime": "2018-10-17T10:42:00+0900"
    }, 
    "copyright": {
        "image": {
            "url": "http://weather.livedoor.com/img/cmn/livedoor.gif", 
            "width": 118, 
            "height": 26, 
            "link": "http://weather.livedoor.com/", 
            "title": "livedoor 天気情報"
        }, 
        "title": "(C) LINE Corporation", 
        "link": "http://weather.livedoor.com/", 
        "provider": [
            {
                "link": "http://tenki.jp/", 
                "name": "日本気象協会"
            }
        ]
    }, 
    "title": "東京都 東京 の天気", 
    "forecasts": [
        {
            "date": "2018-10-17", 
            "image": {
                "url": "http://weather.livedoor.com/img/icon/5.gif", 
                "width": 50, 
                "height": 31, 
                "title": "晴のち曇"
            }, 
            "telop": "晴のち曇", 
            "temperature": {
                "max": {
                    "fahrenheit": "71.6", 
                    "celsius": "22"
                }, 
                "min": null
            }, 
            "dateLabel": "今日"
        }, 
        {
            "date": "2018-10-18", 
            "image": {
                "url": "http://weather.livedoor.com/img/icon/13.gif", 
                "width": 50, 
                "height": 31, 
                "title": "曇のち雨"
            }, 
            "telop": "曇のち雨", 
            "temperature": {
                "max": {
                    "fahrenheit": "64.4", 
                    "celsius": "18"
                }, 
                "min": {
                    "fahrenheit": "59.0", 
                    "celsius": "15"
                }
            }, 
            "dateLabel": "明日"
        }, 
        {
            "date": "2018-10-19", 
            "image": {
                "url": "http://weather.livedoor.com/img/icon/10.gif", 
                "width": 50, 
                "height": 31, 
                "title": "曇時々雨"
            }, 
            "telop": "曇時々雨", 
            "temperature": {
                "max": null, 
                "min": null
            }, 
            "dateLabel": "明後日"
        }
    ], 
    "publicTime": "2018-10-17T11:00:00+0900", 
    "link": "http://weather.livedoor.com/area/forecast/130010", 
    "location": {
        "city": "東京", 
        "prefecture": "東京都", 
        "area": "関東"
    }
}

便利。

17
14
1

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
17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?