JSON

JSON is a format for representing various types of data (numbers, lists, dictionaries etc.) in the form of text strings. The benefit of encoding data in this way is that strings can be be easily stored in text files or shared over the Internet.

JSON encoded data is simple to read and understand. For example, a list [1, 2, 3] in the JSON format becomes simply a string '[1, 2, 3]', and a dictionary {'x': 1, 'y': 2} becomes a string '{"x": 1, "y": 2}'. Beside lists and dictionaries, JSON recognizes only a few basic data types: strings, numbers, true, false, and null. However, since JSON lists and dictionaries can be arbitrarily nested (so we can have e.g. a list of dictionaries or a dictionary whose values are other dictionaries and lists), this syntax can be used to represent complex data structures.

The json module

The Python module json provides tools for reading and writing JSON data.

[1]:
import json

Below is a brief overview of the most commonly used functions in this module. See the json module documentation for details.


json.dumps(obj)

json.dumps takes as an argument an Python object obj and returns a string representing the object in the JSON format.

Examples.

[2]:
my_list = [1, 2, 3, 'Buffalo', -7.5]
json.dumps(my_list)
[2]:
'[1, 2, 3, "Buffalo", -7.5]'
[3]:
student = {"name": "Maggie", "age": 19, "grades": ["A", "B+", "A-"]}
jstudent = json.dumps(student)
jstudent
[3]:
'{"name": "Maggie", "age": 19, "grades": ["A", "B+", "A-"]}'

json.loads(s)

This function is inverse of json.dumps: it takes a JSON encoded string s and returns the corresponding Python object:

[4]:
d = json.loads(jstudent)
d
[4]:
{'name': 'Maggie', 'age': 19, 'grades': ['A', 'B+', 'A-']}
[5]:
d['name']
[5]:
'Maggie'

json.dump(obj, fp)

json.dump is similar to json.dumps but instead of producing a string representing obj, it writes this string to a file object fp. For example, the code below saves the dictionary student to a text file student.json:

[5]:
student = {"name": "Maggie", "age": 19, "grades": ["A", "B+", "A-"]}

with open('student.json', 'w') as f:
    json.dump(student, f)

Here is a link to the file produced in this way: student.json.


json.load(fp)

json.load in turn, is similar to json.loads, but it takes as its argument a file object fp which contains a JSON formatted string, and returns the corresponding Python object:

[6]:
with open('student.json') as f:
    d = json.load(f)
d
[6]:
{'name': 'Maggie', 'age': 19, 'grades': ['A', 'B+', 'A-']}

Example: currency exchange rates

The website www.exchangerate-api.com provides current currency exchange rates. We can use it, for example, to obtain the latest exchange rates against the US dollar:

[3]:
import requests
rates = requests.get('https://open.er-api.com/v6/latest/USD').text

print(rates)
{"result":"success","provider":"https://www.exchangerate-api.com","documentation":"https://www.excha
ngerate-api.com/docs/free","terms_of_use":"https://www.exchangerate-api.com/terms","time_last_update
_unix":1648252952,"time_last_update_utc":"Sat, 26 Mar 2022 00:02:32 +0000","time_next_update_unix":1
648340702,"time_next_update_utc":"Sun, 27 Mar 2022 00:25:02 +0000","time_eol_unix":0,"base_code":"US
D","rates":{"USD":1,"AED":3.67,"AFN":87.38,"ALL":111.39,"AMD":490.51,"ANG":1.79,"AOA":456.53,"ARS":1
10.14,"AUD":1.33,"AWG":1.79,"AZN":1.7,"BAM":1.78,"BBD":2,"BDT":85.26,"BGN":1.78,"BHD":0.376,"BIF":20
09.87,"BMD":1,"BND":1.36,"BOB":6.86,"BRL":4.8,"BSD":1,"BTN":76.25,"BWP":11.51,"BYN":3,"BZD":2,"CAD":
1.25,"CDF":1991.18,"CHF":0.93,"CLP":789.57,"CNY":6.38,"COP":3763.57,"CRC":655.56,"CUP":24,"CVE":100.
34,"CZK":22.37,"DJF":177.72,"DKK":6.79,"DOP":54.78,"DZD":142.67,"EGP":18.41,"ERN":15,"ETB":51.12,"EU
R":0.91,"FJD":2.09,"FKP":0.759,"FOK":6.79,"GBP":0.759,"GEL":3.22,"GGP":0.759,"GHS":8.18,"GIP":0.759,
"GMD":54.77,"GNF":8915.07,"GTQ":7.67,"GYD":208.82,"HKD":7.83,"HNL":24.51,"HRK":6.86,"HTG":106.14,"HU
F":339.1,"IDR":14259.38,"ILS":3.22,"IMP":0.759,"INR":76.26,"IQD":1456.89,"IRR":41942.62,"ISK":129.06
,"JEP":0.759,"JMD":153.19,"JOD":0.709,"JPY":122.12,"KES":115,"KGS":99.06,"KHR":4039.24,"KID":1.33,"K
MF":447.67,"KRW":1221.69,"KWD":0.3,"KYD":0.833,"KZT":494.28,"LAK":12882.01,"LBP":1507.5,"LKR":289.07
,"LRD":153.09,"LSL":14.54,"LYD":4.66,"MAD":9.49,"MDL":18.35,"MGA":3098.09,"MKD":56.14,"MMK":1763.21,
"MNT":2924.78,"MOP":8.07,"MRU":36.33,"MUR":44.04,"MVR":15.42,"MWK":820.11,"MXN":20.03,"MYR":4.21,"MZ
N":64.25,"NAD":14.54,"NGN":414.99,"NIO":35.71,"NOK":8.63,"NPR":122.01,"NZD":1.44,"OMR":0.384,"PAB":1
,"PEN":3.73,"PGK":3.52,"PHP":52.16,"PKR":181.75,"PLN":4.28,"PYG":7121.96,"QAR":3.64,"RON":4.49,"RSD"
:107.08,"RUB":98.53,"RWF":1046.19,"SAR":3.75,"SBD":7.95,"SCR":14.39,"SDG":445.81,"SEK":9.41,"SGD":1.
36,"SHP":0.759,"SLL":11643.18,"SOS":577.43,"SRD":20.61,"SSP":430.22,"STN":22.29,"SYP":2510.64,"SZL":
14.54,"THB":33.56,"TJS":12.56,"TMT":3.5,"TND":2.77,"TOP":2.23,"TRY":14.82,"TTD":6.78,"TVD":1.33,"TWD
":28.61,"TZS":2320.18,"UAH":29.58,"UGX":3596.24,"UYU":41.93,"UZS":11460.98,"VES":4.34,"VND":22860.03
,"VUV":112.24,"WST":2.56,"XAF":596.89,"XCD":2.7,"XDR":0.725,"XOF":596.89,"XPF":108.59,"YER":250.17,"
ZAR":14.54,"ZMW":17.86,"ZWL":138.14}}

The exchange rates are provided as a JSON encoded string. Using the json.loads function we can convert this string into a Python dictionary:

[5]:
rates_d = json.loads(rates)
rates_d
[5]:
{'result': 'success',
 'provider': 'https://www.exchangerate-api.com',
 'documentation': 'https://www.exchangerate-api.com/docs/free',
 'terms_of_use': 'https://www.exchangerate-api.com/terms',
 'time_last_update_unix': 1648252952,
 'time_last_update_utc': 'Sat, 26 Mar 2022 00:02:32 +0000',
 'time_next_update_unix': 1648340702,
 'time_next_update_utc': 'Sun, 27 Mar 2022 00:25:02 +0000',
 'time_eol_unix': 0,
 'base_code': 'USD',
 'rates': {'USD': 1,
  'AED': 3.67,
  'AFN': 87.38,
  'ALL': 111.39,
  'AMD': 490.51,
  'ANG': 1.79,
  'AOA': 456.53,
  'ARS': 110.14,
  'AUD': 1.33,
  'AWG': 1.79,
  'AZN': 1.7,
  'BAM': 1.78,
  'BBD': 2,
  'BDT': 85.26,
  'BGN': 1.78,
  'BHD': 0.376,
  'BIF': 2009.87,
  'BMD': 1,
  'BND': 1.36,
  'BOB': 6.86,
  'BRL': 4.8,
  'BSD': 1,
  'BTN': 76.25,
  'BWP': 11.51,
  'BYN': 3,
  'BZD': 2,
  'CAD': 1.25,
  'CDF': 1991.18,
  'CHF': 0.93,
  'CLP': 789.57,
  'CNY': 6.38,
  'COP': 3763.57,
  'CRC': 655.56,
  'CUP': 24,
  'CVE': 100.34,
  'CZK': 22.37,
  'DJF': 177.72,
  'DKK': 6.79,
  'DOP': 54.78,
  'DZD': 142.67,
  'EGP': 18.41,
  'ERN': 15,
  'ETB': 51.12,
  'EUR': 0.91,
  'FJD': 2.09,
  'FKP': 0.759,
  'FOK': 6.79,
  'GBP': 0.759,
  'GEL': 3.22,
  'GGP': 0.759,
  'GHS': 8.18,
  'GIP': 0.759,
  'GMD': 54.77,
  'GNF': 8915.07,
  'GTQ': 7.67,
  'GYD': 208.82,
  'HKD': 7.83,
  'HNL': 24.51,
  'HRK': 6.86,
  'HTG': 106.14,
  'HUF': 339.1,
  'IDR': 14259.38,
  'ILS': 3.22,
  'IMP': 0.759,
  'INR': 76.26,
  'IQD': 1456.89,
  'IRR': 41942.62,
  'ISK': 129.06,
  'JEP': 0.759,
  'JMD': 153.19,
  'JOD': 0.709,
  'JPY': 122.12,
  'KES': 115,
  'KGS': 99.06,
  'KHR': 4039.24,
  'KID': 1.33,
  'KMF': 447.67,
  'KRW': 1221.69,
  'KWD': 0.3,
  'KYD': 0.833,
  'KZT': 494.28,
  'LAK': 12882.01,
  'LBP': 1507.5,
  'LKR': 289.07,
  'LRD': 153.09,
  'LSL': 14.54,
  'LYD': 4.66,
  'MAD': 9.49,
  'MDL': 18.35,
  'MGA': 3098.09,
  'MKD': 56.14,
  'MMK': 1763.21,
  'MNT': 2924.78,
  'MOP': 8.07,
  'MRU': 36.33,
  'MUR': 44.04,
  'MVR': 15.42,
  'MWK': 820.11,
  'MXN': 20.03,
  'MYR': 4.21,
  'MZN': 64.25,
  'NAD': 14.54,
  'NGN': 414.99,
  'NIO': 35.71,
  'NOK': 8.63,
  'NPR': 122.01,
  'NZD': 1.44,
  'OMR': 0.384,
  'PAB': 1,
  'PEN': 3.73,
  'PGK': 3.52,
  'PHP': 52.16,
  'PKR': 181.75,
  'PLN': 4.28,
  'PYG': 7121.96,
  'QAR': 3.64,
  'RON': 4.49,
  'RSD': 107.08,
  'RUB': 98.53,
  'RWF': 1046.19,
  'SAR': 3.75,
  'SBD': 7.95,
  'SCR': 14.39,
  'SDG': 445.81,
  'SEK': 9.41,
  'SGD': 1.36,
  'SHP': 0.759,
  'SLL': 11643.18,
  'SOS': 577.43,
  'SRD': 20.61,
  'SSP': 430.22,
  'STN': 22.29,
  'SYP': 2510.64,
  'SZL': 14.54,
  'THB': 33.56,
  'TJS': 12.56,
  'TMT': 3.5,
  'TND': 2.77,
  'TOP': 2.23,
  'TRY': 14.82,
  'TTD': 6.78,
  'TVD': 1.33,
  'TWD': 28.61,
  'TZS': 2320.18,
  'UAH': 29.58,
  'UGX': 3596.24,
  'UYU': 41.93,
  'UZS': 11460.98,
  'VES': 4.34,
  'VND': 22860.03,
  'VUV': 112.24,
  'WST': 2.56,
  'XAF': 596.89,
  'XCD': 2.7,
  'XDR': 0.725,
  'XOF': 596.89,
  'XPF': 108.59,
  'YER': 250.17,
  'ZAR': 14.54,
  'ZMW': 17.86,
  'ZWL': 138.14}}

We can use the dictionary to check how much 1 USD is worth in terms of any other listed currency:

[6]:
# value of 1 USD in Canadian dollars
rates_d['rates']['CAD']
[6]:
1.25