PythonでのJSON利用例 見た目を整える

JSONは、データをコンピュータも人も扱いやすい形にするものです。
今回は人が見やすい形に変更します。

今回扱うデータ

MetaWeather 天気予報のWeb APIからのデータを、requestsモジュールを利用して読み込みます。
location/1118370は東京を表すコードです。

>>> import requests
>>> r = requests.get('https://www.metaweather.com/api/location/1118370')

まず変数”r”にデータを格納してステータスを確認。

>>> r.status_code
200
>>> r.headers['content-type']
'application/json'
>>> r.encoding
'utf-8'

Status Codeが200と言う事は、サーバーへのrequestがきちんと届き、求めたデータが返された事を示しています。
content-typeが’appication/json’になっているので、Web APIがデータを扱う為のjson形式のデータ、と言う事が分かります。
json形式と言う事はencodingはutf-8のはずですが、確かにその通りになっています。

r.textで出力するとjson形式そのままで吐き出され、中身を見る事が出来ますが、このままでは人間の目では見にくい。

因みに、下記codeが書かれた箱の上、左から3つめ”Toggle Line Wrap”をクリックすると、コードが折りたたまれて全文を一覧しやすくなります。

>>> r.text
'{"consolidated_weather":[{"id":4534167229431808,"weather_state_name":"Light Rain","weather_state_abbr":"lr","wind_direction_compass":"NNE","created":"2021-06-20T03:36:46.125447Z","applicable_date":"2021-06-20","min_temp":19.79,"max_temp":26.205,"the_temp":26.89,"wind_speed":7.630599006132189,"wind_direction":29.000000000000004,"air_pressure":998.0,"humidity":73,"visibility":11.583912451284498,"predictability":75},{"id":5208805794119680,"weather_state_name":"Showers","weather_state_abbr":"s","wind_direction_compass":"SSE","created":"2021-06-20T03:36:49.450941Z","applicable_date":"2021-06-21","min_temp":21.284999999999997,"max_temp":27.705,"the_temp":26.525,"wind_speed":6.444322981887113,"wind_direction":148.37889841508593,"air_pressure":1004.5,"humidity":57,"visibility":13.370043943370716,"predictability":73},{"id":5174934071410688,"weather_state_name":"Showers","weather_state_abbr":"s","wind_direction_compass":"S","created":"2021-06-20T03:36:52.006442Z","applicable_date":"2021-06-22","min_temp":21.43,"max_temp":26.299999999999997,"the_temp":25.28,"wind_speed":7.890906107919086,"wind_direction":169.45400062966624,"air_pressure":1008.5,"humidity":66,"visibility":11.549426350115326,"predictability":73},{"id":4797483621285888,"weather_state_name":"Light Rain","weather_state_abbr":"lr","wind_direction_compass":"ENE","created":"2021-06-20T03:36:54.938740Z","applicable_date":"2021-06-23","min_temp":20.82,"max_temp":26.18,"the_temp":25.075,"wind_speed":6.386218420642117,"wind_direction":77.0,"air_pressure":1010.0,"humidity":64,"visibility":11.041144714865187,"predictability":75},{"id":6085661653204992,"weather_state_name":"Showers","weather_state_abbr":"s","wind_direction_compass":"NE","created":"2021-06-20T03:36:57.973908Z","applicable_date":"2021-06-24","min_temp":20.335,"max_temp":25.049999999999997,"the_temp":24.630000000000003,"wind_speed":7.422489563464037,"wind_direction":54.21205312696262,"air_pressure":1010.5,"humidity":68,"visibility":10.834538793446274,"predictability":73},{"id":6252981063254016,"weather_state_name":"Heavy Rain","weather_state_abbr":"hr","wind_direction_compass":"NNE","created":"2021-06-20T03:37:01.972644Z","applicable_date":"2021-06-25","min_temp":19.67,"max_temp":23.615000000000002,"the_temp":20.18,"wind_speed":5.359549033643521,"wind_direction":29.499999999999996,"air_pressure":1011.0,"humidity":84,"visibility":8.887472162570587,"predictability":77}],"time":"2021-06-20T15:20:16.797014+09:00","sun_rise":"2021-06-20T04:25:30.740096+09:00","sun_set":"2021-06-20T18:59:53.501819+09:00","timezone_name":"JST","parent":{"title":"Japan","location_type":"Country","woeid":23424856,"latt_long":"37.487598,139.838287"},"sources":[{"title":"BBC","slug":"bbc","url":"http://www.bbc.co.uk/weather/","crawl_rate":360},{"title":"Forecast.io","slug":"forecast-io","url":"http://forecast.io/","crawl_rate":480},{"title":"HAMweather","slug":"hamweather","url":"http://www.hamweather.com/","crawl_rate":360},{"title":"Met Office","slug":"met-office","url":"http://www.metoffice.gov.uk/","crawl_rate":180},{"title":"OpenWeatherMap","slug":"openweathermap","url":"http://openweathermap.org/","crawl_rate":360},{"title":"Weather Underground","slug":"wunderground","url":"https://www.wunderground.com/?apiref=fc30dc3cd224e19b","crawl_rate":720},{"title":"World Weather Online","slug":"world-weather-online","url":"http://www.worldweatheronline.com/","crawl_rate":360}],"title":"Tokyo","location_type":"City","woeid":1118370,"latt_long":"35.670479,139.740921","timezone":"Asia/Tokyo"}'

まずはDecodeして扱いやすいPython形式に変える

ここでr.text => r.json()とすると、rのmethod “r.json()”でjson形式からPythonのdictionaryのデータ型にDecode(Deserialization)されます。
因みに、このrは

r = requests.get('https://www.metaweather.com/api/location/1118370')

のrであり、request.getで得られたObject(Response Objectと呼ばれている)になります。

これをただr.json()で出力すると下記の通り。

>>> r.json()
{'consolidated_weather': [{'id': 4534167229431808, 'weather_state_name': 'Light Rain', 'weather_state_abbr': 'lr', 'wind_direction_compass': 'NNE', 'created': '2021-06-20T03:36:46.125447Z', 'applicable_date': '2021-06-20', 'min_temp': 19.79, 'max_temp': 26.205, 'the_temp': 26.89, 'wind_speed': 7.630599006132189, 'wind_direction': 29.000000000000004, 'air_pressure': 998.0, 'humidity': 73, 'visibility': 11.583912451284498, 'predictability': 75}, {'id': 5208805794119680, 'weather_state_name': 'Showers', 'weather_state_abbr': 's', 'wind_direction_compass': 'SSE', 'created': '2021-06-20T03:36:49.450941Z', 'applicable_date': '2021-06-21', 'min_temp': 21.284999999999997, 'max_temp': 27.705, 'the_temp': 26.525, 'wind_speed': 6.444322981887113, 'wind_direction': 148.37889841508593, 'air_pressure': 1004.5, 'humidity': 57, 'visibility': 13.370043943370716, 'predictability': 73}, {'id': 5174934071410688, 'weather_state_name': 'Showers', 'weather_state_abbr': 's', 'wind_direction_compass': 'S', 'created': '2021-06-20T03:36:52.006442Z', 'applicable_date': '2021-06-22', 'min_temp': 21.43, 'max_temp': 26.299999999999997, 'the_temp': 25.28, 'wind_speed': 7.890906107919086, 'wind_direction': 169.45400062966624, 'air_pressure': 1008.5, 'humidity': 66, 'visibility': 11.549426350115326, 'predictability': 73}, {'id': 4797483621285888, 'weather_state_name': 'Light Rain', 'weather_state_abbr': 'lr', 'wind_direction_compass': 'ENE', 'created': '2021-06-20T03:36:54.938740Z', 'applicable_date': '2021-06-23', 'min_temp': 20.82, 'max_temp': 26.18, 'the_temp': 25.075, 'wind_speed': 6.386218420642117, 'wind_direction': 77.0, 'air_pressure': 1010.0, 'humidity': 64, 'visibility': 11.041144714865187, 'predictability': 75}, {'id': 6085661653204992, 'weather_state_name': 'Showers', 'weather_state_abbr': 's', 'wind_direction_compass': 'NE', 'created': '2021-06-20T03:36:57.973908Z', 'applicable_date': '2021-06-24', 'min_temp': 20.335, 'max_temp': 25.049999999999997, 'the_temp': 24.630000000000003, 'wind_speed': 7.422489563464037, 'wind_direction': 54.21205312696262, 'air_pressure': 1010.5, 'humidity': 68, 'visibility': 10.834538793446274, 'predictability': 73}, {'id': 6252981063254016, 'weather_state_name': 'Heavy Rain', 'weather_state_abbr': 'hr', 'wind_direction_compass': 'NNE', 'created': '2021-06-20T03:37:01.972644Z', 'applicable_date': '2021-06-25', 'min_temp': 19.67, 'max_temp': 23.615000000000002, 'the_temp': 20.18, 'wind_speed': 5.359549033643521, 'wind_direction': 29.499999999999996, 'air_pressure': 1011.0, 'humidity': 84, 'visibility': 8.887472162570587, 'predictability': 77}], 'time': '2021-06-20T15:20:16.797014+09:00', 'sun_rise': '2021-06-20T04:25:30.740096+09:00', 'sun_set': '2021-06-20T18:59:53.501819+09:00', 'timezone_name': 'JST', 'parent': {'title': 'Japan', 'location_type': 'Country', 'woeid': 23424856, 'latt_long': '37.487598,139.838287'}, 'sources': [{'title': 'BBC', 'slug': 'bbc', 'url': 'http://www.bbc.co.uk/weather/', 'crawl_rate': 360}, {'title': 'Forecast.io', 'slug': 'forecast-io', 'url': 'http://forecast.io/', 'crawl_rate': 480}, {'title': 'HAMweather', 'slug': 'hamweather', 'url': 'http://www.hamweather.com/', 'crawl_rate': 360}, {'title': 'Met Office', 'slug': 'met-office', 'url': 'http://www.metoffice.gov.uk/', 'crawl_rate': 180}, {'title': 'OpenWeatherMap', 'slug': 'openweathermap', 'url': 'http://openweathermap.org/', 'crawl_rate': 360}, {'title': 'Weather Underground', 'slug': 'wunderground', 'url': 'https://www.wunderground.com/?apiref=fc30dc3cd224e19b', 'crawl_rate': 720}, {'title': 'World Weather Online', 'slug': 'world-weather-online', 'url': 'http://www.worldweatheronline.com/', 'crawl_rate': 360}], 'title': 'Tokyo', 'location_type': 'City', 'woeid': 1118370, 'latt_long': '35.670479,139.740921', 'timezone': 'Asia/Tokyo'}


一見、区切りがダブルクォーテーション「”」からシングルクォーテーション「’」に変わり、最初と最後の「’」クオーテーションマークが取れただけの同じようなデータに見えますが、実際はPythonのdictionary型データであり、扱い易くなっています。

Encodeをして見た目を整える

上記でjson型のデータをPythonのdictionary型にdecodeしたので、今からPythonのjsonモジュールを使用して見易くします。

変数rjに、元データ(json型)からdictionary型データにdecodeされたr.json()から、json.dumpsでEncodeしたデータを格納します。
その際、「indent=4」の様にindentを入れます。

>>> import json
>>> rj = json.dumps(r.json(), indent=4)

このrjをただ出力すると、同じ様な見にくいデータのままですが、見ると所々に改行「\n」が入っていることが分かります。

>>> rj
'{\n    "consolidated_weather": [\n        {\n            "id": 6102397261709312,\n            "weather_state_name": "Light Rain",\n            "weather_state_abbr": "lr",\n            "wind_direction_compass": "NNE",\n            "created": "2021-06-20T12:36:47.257802Z",\n            "applicable_date": "2021-06-20",\n            "min_temp": 19.935,\n            "max_temp": 27.22,\n            "the_temp": 26.630000000000003,\n            "wind_speed": 7.182764119594899,\n            "wind_direction": 28.500000000000007,\n            "air_pressure": 998.0,\n            "humidity": 70,\n            "visibility": 12.777877197168536,\n            "predictability": 75\n        },\n        {\n            "id": 4728843366563840,\n            "weather_state_name": "Light Rain",\n            "weather_state_abbr": "lr",\n            "wind_direction_compass": "SE",\n            "created": "2021-06-20T12:36:50.227358Z",\n            "applicable_date": "2021-06-21",\n            "min_temp": 20.95,\n            "max_temp": 26.695,\n            "the_temp": 26.189999999999998,\n            "wind_speed": 6.865762760969273,\n            "wind_direction": 135.31537751253472,\n            "air_pressure": 1005.0,\n            "humidity": 61,\n            "visibility": 13.416957468384634,\n            "predictability": 75\n        },\n        {\n            "id": 6154227517751296,\n            "weather_state_name": "Light Rain",\n            "weather_state_abbr": "lr",\n            "wind_direction_compass": "SSE",\n            "created": "2021-06-20T12:36:53.134335Z",\n            "applicable_date": "2021-06-22",\n            "min_temp": 20.275,\n            "max_temp": 26.965,\n            "the_temp": 25.47,\n            "wind_speed": 6.456194872952625,\n            "wind_direction": 153.04371766880197,\n            "air_pressure": 1008.0,\n            "humidity": 65,\n            "visibility": 12.072310208383044,\n            "predictability": 75\n        },\n        {\n            "id": 6213293283737600,\n            "weather_state_name": "Light Rain",\n            "weather_state_abbr": "lr",\n            "wind_direction_compass": "ENE",\n            "created": "2021-06-20T12:36:56.308709Z",\n            "applicable_date": "2021-06-23",\n            "min_temp": 20.47,\n            "max_temp": 26.265,\n            "the_temp": 25.56,\n            "wind_speed": 7.1646883915294675,\n            "wind_direction": 74.16831695049069,\n            "air_pressure": 1009.5,\n            "humidity": 61,\n            "visibility": 11.8169266483735,\n            "predictability": 75\n        },\n        {\n            "id": 4857742415101952,\n            "weather_state_name": "Showers",\n            "weather_state_abbr": "s",\n            "wind_direction_compass": "ENE",\n            "created": "2021-06-20T12:36:59.730423Z",\n            "applicable_date": "2021-06-24",\n            "min_temp": 19.854999999999997,\n            "max_temp": 27.475,\n            "the_temp": 26.450000000000003,\n            "wind_speed": 6.2296495297303744,\n            "wind_direction": 65.59226935085387,\n            "air_pressure": 1011.0,\n            "humidity": 59,\n            "visibility": 12.645214447625865,\n            "predictability": 73\n        },\n        {\n            "id": 4946185220521984,\n            "weather_state_name": "Heavy Rain",\n            "weather_state_abbr": "hr",\n            "wind_direction_compass": "NNE",\n            "created": "2021-06-20T12:37:02.334879Z",\n            "applicable_date": "2021-06-25",\n            "min_temp": 18.965,\n            "max_temp": 23.72,\n            "the_temp": 18.94,\n            "wind_speed": 6.0477083830430285,\n            "wind_direction": 27.0,\n            "air_pressure": 1010.0,\n            "humidity": 87,\n            "visibility": 8.288470333253798,\n            "predictability": 77\n        }\n    ],\n    "time": "2021-06-20T23:46:38.256891+09:00",\n    "sun_rise": "2021-06-20T04:25:30.740096+09:00",\n    "sun_set": "2021-06-20T18:59:53.501819+09:00",\n    "timezone_name": "JST",\n    "parent": {\n        "title": "Japan",\n        "location_type": "Country",\n        "woeid": 23424856,\n        "latt_long": "37.487598,139.838287"\n    },\n    "sources": [\n        {\n            "title": "BBC",\n            "slug": "bbc",\n            "url": "http://www.bbc.co.uk/weather/",\n            "crawl_rate": 360\n        },\n        {\n            "title": "Forecast.io",\n            "slug": "forecast-io",\n            "url": "http://forecast.io/",\n            "crawl_rate": 480\n        },\n        {\n            "title": "HAMweather",\n            "slug": "hamweather",\n            "url": "http://www.hamweather.com/",\n            "crawl_rate": 360\n        },\n        {\n            "title": "Met Office",\n            "slug": "met-office",\n            "url": "http://www.metoffice.gov.uk/",\n            "crawl_rate": 180\n        },\n        {\n            "title": "OpenWeatherMap",\n            "slug": "openweathermap",\n            "url": "http://openweathermap.org/",\n            "crawl_rate": 360\n        },\n        {\n            "title": "Weather Underground",\n            "slug": "wunderground",\n            "url": "https://www.wunderground.com/?apiref=fc30dc3cd224e19b",\n            "crawl_rate": 720\n        },\n        {\n            "title": "World Weather Online",\n            "slug": "world-weather-online",\n            "url": "http://www.worldweatheronline.com/",\n            "crawl_rate": 360\n        }\n    ],\n    "title": "Tokyo",\n    "location_type": "City",\n    "woeid": 1118370,\n    "latt_long": "35.670479,139.740921",\n    "timezone": "Asia/Tokyo"\n}'

最初と最後の{}の外にシングルクォーテーション「’」がある事から、Pythonのdictionary型からjson形式データにEncodeされた事が分かります。

このrjをprint()で出力すると、indent=4で整えられた人間の目で見やすいデータになりました。

>>> print(rj)
{
    "consolidated_weather": [
        {
            "id": 6102397261709312,
            "weather_state_name": "Light Rain",
            "weather_state_abbr": "lr",
            "wind_direction_compass": "NNE",
            "created": "2021-06-20T12:36:47.257802Z",
            "applicable_date": "2021-06-20",
            "min_temp": 19.935,
            "max_temp": 27.22,
            "the_temp": 26.630000000000003,
            "wind_speed": 7.182764119594899,
            "wind_direction": 28.500000000000007,
            "air_pressure": 998.0,
            "humidity": 70,
            "visibility": 12.777877197168536,
            "predictability": 75
        },
        {
            "id": 4728843366563840,
            "weather_state_name": "Light Rain",
            "weather_state_abbr": "lr",
            "wind_direction_compass": "SE",
            "created": "2021-06-20T12:36:50.227358Z",
            "applicable_date": "2021-06-21",
            "min_temp": 20.95,
            "max_temp": 26.695,
            "the_temp": 26.189999999999998,
            "wind_speed": 6.865762760969273,
            "wind_direction": 135.31537751253472,
            "air_pressure": 1005.0,
            "humidity": 61,
            "visibility": 13.416957468384634,
            "predictability": 75
        },
        {
            "id": 6154227517751296,
            "weather_state_name": "Light Rain",
            "weather_state_abbr": "lr",
            "wind_direction_compass": "SSE",
            "created": "2021-06-20T12:36:53.134335Z",
            "applicable_date": "2021-06-22",
            "min_temp": 20.275,
            "max_temp": 26.965,
            "the_temp": 25.47,
            "wind_speed": 6.456194872952625,
            "wind_direction": 153.04371766880197,
            "air_pressure": 1008.0,
            "humidity": 65,
            "visibility": 12.072310208383044,
            "predictability": 75
        },
        {
            "id": 6213293283737600,
            "weather_state_name": "Light Rain",
            "weather_state_abbr": "lr",
            "wind_direction_compass": "ENE",
            "created": "2021-06-20T12:36:56.308709Z",
            "applicable_date": "2021-06-23",
            "min_temp": 20.47,
            "max_temp": 26.265,
            "the_temp": 25.56,
            "wind_speed": 7.1646883915294675,
            "wind_direction": 74.16831695049069,
            "air_pressure": 1009.5,
            "humidity": 61,
            "visibility": 11.8169266483735,
            "predictability": 75
        },
        {
            "id": 4857742415101952,
            "weather_state_name": "Showers",
            "weather_state_abbr": "s",
            "wind_direction_compass": "ENE",
            "created": "2021-06-20T12:36:59.730423Z",
            "applicable_date": "2021-06-24",
            "min_temp": 19.854999999999997,
            "max_temp": 27.475,
            "the_temp": 26.450000000000003,
            "wind_speed": 6.2296495297303744,
            "wind_direction": 65.59226935085387,
            "air_pressure": 1011.0,
            "humidity": 59,
            "visibility": 12.645214447625865,
            "predictability": 73
        },
        {
            "id": 4946185220521984,
            "weather_state_name": "Heavy Rain",
            "weather_state_abbr": "hr",
            "wind_direction_compass": "NNE",
            "created": "2021-06-20T12:37:02.334879Z",
            "applicable_date": "2021-06-25",
            "min_temp": 18.965,
            "max_temp": 23.72,
            "the_temp": 18.94,
            "wind_speed": 6.0477083830430285,
            "wind_direction": 27.0,
            "air_pressure": 1010.0,
            "humidity": 87,
            "visibility": 8.288470333253798,
            "predictability": 77
        }
    ],
    "time": "2021-06-20T23:46:38.256891+09:00",
    "sun_rise": "2021-06-20T04:25:30.740096+09:00",
    "sun_set": "2021-06-20T18:59:53.501819+09:00",
    "timezone_name": "JST",
    "parent": {
        "title": "Japan",
        "location_type": "Country",
        "woeid": 23424856,
        "latt_long": "37.487598,139.838287"
    },
    "sources": [
        {
            "title": "BBC",
            "slug": "bbc",
            "url": "http://www.bbc.co.uk/weather/",
            "crawl_rate": 360
        },
        {
            "title": "Forecast.io",
            "slug": "forecast-io",
            "url": "http://forecast.io/",
            "crawl_rate": 480
        },
        {
            "title": "HAMweather",
            "slug": "hamweather",
            "url": "http://www.hamweather.com/",
            "crawl_rate": 360
        },
        {
            "title": "Met Office",
            "slug": "met-office",
            "url": "http://www.metoffice.gov.uk/",
            "crawl_rate": 180
        },
        {
            "title": "OpenWeatherMap",
            "slug": "openweathermap",
            "url": "http://openweathermap.org/",
            "crawl_rate": 360
        },
        {
            "title": "Weather Underground",
            "slug": "wunderground",
            "url": "https://www.wunderground.com/?apiref=fc30dc3cd224e19b",
            "crawl_rate": 720
        },
        {
            "title": "World Weather Online",
            "slug": "world-weather-online",
            "url": "http://www.worldweatheronline.com/",
            "crawl_rate": 360
        }
    ],
    "title": "Tokyo",
    "location_type": "City",
    "woeid": 1118370,
    "latt_long": "35.670479,139.740921",
    "timezone": "Asia/Tokyo"
}

う〜ん、見やすさが段違い。
元のtextデータに一つ一つ改行などしていたらどれだけの時間が掛かるのか想像もしたくないですね。

今回やった事

Web Apiからjson形式で取得したデータを

requestモジュールを使用して扱いやすいPython型にDocodeした上で

jsonモジュールのjson.dumps()で見やすいjson形式データにEncodeして出力

今回はJSONモジュールを使って人の目で見やすい形式にしました。
次はコンピューターが扱いやすい形式に。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

1 × five =

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください