Watch Me Code 1: JSON


In [1]:
import json

with open('WMC4-usinfo.json', encoding='utf8') as inf:
    data = inf.read()
    
data


Out[1]:
'[{"name":"United States","topLevelDomain":[".us"],"alpha2Code":"US","alpha3Code":"USA","callingCodes":["1"],"capital":"Washington, D.C.","altSpellings":["US","USA","United States of America"],"relevance":"3.5","region":"Americas","subregion":"Northern America","translations":{"de":"Vereinigte Staaten von Amerika","es":"Estados Unidos","fr":"États-Unis","ja":"アメリカ合衆国","it":"Stati Uniti D\'America"},"population":321645000,"latlng":[38.0,-97.0],"demonym":"American","area":9629091.0,"gini":48.0,"timezones":["UTC-12:00","UTC-11:00","UTC-10:00","UTC-09:00","UTC-08:00","UTC-07:00","UTC-06:00","UTC-05:00","UTC-04:00","UTC+10:00","UTC+12:00"],"borders":["CAN","MEX"],"nativeName":"United States","numericCode":"840","currencies":["USD","USN","USS"],"languages":["en"]}]'

In [20]:
type(data)


Out[20]:
str

In [2]:
# let's decode the string to a Python object
us = json.loads(data)

us


Out[2]:
[{'alpha2Code': 'US',
  'alpha3Code': 'USA',
  'altSpellings': ['US', 'USA', 'United States of America'],
  'area': 9629091.0,
  'borders': ['CAN', 'MEX'],
  'callingCodes': ['1'],
  'capital': 'Washington, D.C.',
  'currencies': ['USD', 'USN', 'USS'],
  'demonym': 'American',
  'gini': 48.0,
  'languages': ['en'],
  'latlng': [38.0, -97.0],
  'name': 'United States',
  'nativeName': 'United States',
  'numericCode': '840',
  'population': 321645000,
  'region': 'Americas',
  'relevance': '3.5',
  'subregion': 'Northern America',
  'timezones': ['UTC-12:00',
   'UTC-11:00',
   'UTC-10:00',
   'UTC-09:00',
   'UTC-08:00',
   'UTC-07:00',
   'UTC-06:00',
   'UTC-05:00',
   'UTC-04:00',
   'UTC+10:00',
   'UTC+12:00'],
  'topLevelDomain': ['.us'],
  'translations': {'de': 'Vereinigte Staaten von Amerika',
   'es': 'Estados Unidos',
   'fr': 'États-Unis',
   'it': "Stati Uniti D'America",
   'ja': 'アメリカ合衆国'}}]

In [12]:
us


Out[12]:
[{'alpha2Code': 'US',
  'alpha3Code': 'USA',
  'altSpellings': ['US', 'USA', 'United States of America'],
  'area': 9629091.0,
  'borders': ['CAN', 'MEX'],
  'callingCodes': ['1'],
  'capital': 'Washington, D.C.',
  'currencies': ['USD', 'USN', 'USS'],
  'demonym': 'American',
  'gini': 48.0,
  'languages': ['en'],
  'latlng': [38.0, -97.0],
  'name': 'United States',
  'nativeName': 'United States',
  'numericCode': '840',
  'population': 321645000,
  'region': 'Americas',
  'relevance': '3.5',
  'subregion': 'Northern America',
  'timezones': ['UTC-12:00',
   'UTC-11:00',
   'UTC-10:00',
   'UTC-09:00',
   'UTC-08:00',
   'UTC-07:00',
   'UTC-06:00',
   'UTC-05:00',
   'UTC-04:00',
   'UTC+10:00',
   'UTC+12:00'],
  'topLevelDomain': ['.us'],
  'translations': {'de': 'Vereinigte Staaten von Amerika',
   'es': 'Estados Unidos',
   'fr': 'États-Unis',
   'it': "Stati Uniti D'America",
   'ja': 'アメリカ合衆国'}}]

In [9]:
population = us[0]["population"]
print(population)


321645000

In [17]:
borders = us[0]['borders']
print(borders)


['CAN', 'MEX']

In [ ]: