0
2

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.

【GAS】初心者向けJSON形式の読み方

Last updated at Posted at 2020-09-12

概要

JSONって形式のデータ記述を扱うことがあるけど、JSONが何なのか良くわからない、ぐちゃっとした文字の羅列で見にくい、しかもGASで読み取るときの書き方が意味分からん!

JSONを一から理解するのはプロに任せて、素人リーマンは取りあえずGASでJSON形式の記述から必要な情報さえ得られれば良いやと思いましたとさ。

エンジニアを目指すのではなく、なんとなく便利に使いたい人向けの記事^^)b

JSON形式の記述例

個人的に、GoogleMapをgeocodeした結果としてJSONにぶち当たる事が多いので、そちらを例として解説。「スカイツリー」をgeocodeしたときに得られるJSON形式の記述が次の通り。

{status=OK, results=[{types=[establishment, point_of_interest, tourist_attraction],
 place_id=ChIJ35ov0dCOGGARKvdDH7NPHX0, plus_code={compound_code=PR66+27 日本、東京都
墨田区, global_code=8Q7XPR66+27}, formatted_address=日本、〒131-8634 東京都墨田区押上
1丁目1−2, geometry={viewport={northeast={lat=35.7114116802915, 
lng=139.8120493802915}, southwest={lat=35.7087137197085, lng=139.8093514197085}}, 
location_type=ROOFTOP, location={lng=139.8107004, lat=35.7100627}}, 
address_components=[{types=[premise], short_name=2, long_name=2}, {long_name=1, 
types=[political, sublocality, sublocality_level_4], short_name=1}, {types=
[political, sublocality, sublocality_level_3], long_name=1丁目, short_name=1丁目}, 
{short_name=押上, types=[political, sublocality, sublocality_level_2], long_name=押
上}, {long_name=墨田区, types=[locality, political], short_name=墨田区}, {short_name=
東京都, types=[administrative_area_level_1, political], long_name=東京都}, {types=
[country, political], short_name=JP, long_name=日本}, {types=[postal_code], 
long_name=131-8634, short_name=131-8634}]}]}

もう嫌・・・。

基本的な考え方

例えば、この中から緯度(lat)を読み取りたいとき、GASでは次のように書く。

['results'][0]['geometry']['location']['lat']

その心は[results[ ]内の] [1個目の{ }内の] [geometryの] [locationの] [lat]の意。JSONの記述を、よーく見るとその通りになっていることが分かる。

よーく見た

改行ポイントはテキトーだけど、自分なりに整理してみた。

results=[{geometry={location={lng=139.8107004, lat=35.7100627}, 
                    location_type=ROOFTOP, 
                    viewport={northeast={lng=139.8120493802915, lat=35.7114116802915}, 
                              southwest={lng=139.8093514197085, lat=35.7087137197085}}}, 
          place_id=ChIJ35ov0dCOGGARKvdDH7NPHX0, 
          address_components=[{long_name=2, short_name=2, types=[premise]}, 
                              {short_name=1, long_name=1, types=[political, sublocality, sublocality_level_4]}, 
                              {types=[political, sublocality, sublocality_level_3], long_name=1丁目, short_name=1丁目}, 
                              {short_name=押上, long_name=押上, types=[political, sublocality, sublocality_level_2]}, 
                              {types=[locality, political], long_name=墨田区, short_name=墨田区}, 
                              {short_name=東京都, long_name=東京都, types=[administrative_area_level_1, political]}, 
                              {short_name=JP, types=[country, political], long_name=日本}, 
                              {types=[postal_code], short_name=131-8634, long_name=131-8634}], 
          plus_code={compound_code=PR66+27 日本、東京都墨田区, global_code=8Q7XPR66+27}, 
          types=[establishment, point_of_interest, tourist_attraction], 
          formatted_address=日本、〒131-8634 東京都墨田区押上1丁目1−2}]

生のJSONよりは頑張れそう(笑)
順番に見ていくと、results[ ]があって、その中の1個目の{ }の中の、geometryの、locationのlat、で確かに緯度まで辿りつける。

練習

郵便番号を読み取るために「short_name=131-8634」までの道のりを探す。緯度と比べて遠いorz こんな時は、自分なりに整理したJSON記述から、不要な部分を削除。

results=[{address_components=[{}, 
                              {}, 
                              {}, 
                              {}, 
                              {}, 
                              {}, 
                              {}, 
                              {short_name=131-8634,}],

何という事でしょう、匠の技で見晴らしがヽ(*´∀`)ノ

先ほどの考え方に当てはめると、results[ ]の中の、1個目の{ }の中の、address_components[ ]の中の、8個目の{ }の中の、short_name、と言う事になるので、GASでの書き方は

['results'][0]['address_components'][7]['short_name']

となるはず。

試してみた

コードはコチラ

function myFunction() {
  
  var geo = Maps.newGeocoder();
  geo.setLanguage('ja');
  var response = geo.geocode('スカイツリー');
  
  var zip = response['results'][0]['address_components'][7]['short_name'];
  
  Logger.log(zip);
  
}

そしてログがコチラ
image.png
どうやら正解!

これでJSON形式の記述も怖くないぜ。+゚ (´∀`)ノ。+゜

おしまい

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?