Introduction to the json module for the Python geek

There are many builtin modules being offered by the Python computer programming language. Each one of them has been built by the developers with the main purpose of solving specific problems.

Through this article, you will learn how to make use of the Python’s json module, so you can encode and decode Python objects through it.

What is JSON

According to the Python’s official documentation, JSON is a lightweight data interchange format inspired by the Javascript object literal syntax.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Practice JSON with the json module

Before going any further with this tutorial, make sure to launch a new Python interactive shell in your own operating system, so you can practice the examples being shared in here, by yourself.

Once you have managed to launch a new Python interactive shell in your own operating system, make use of the builtin import statement to include the json module in your session.

import json

Then define a dictionary object by making use of the Python’s syntax shown below.

d = {'name': 'oltjano', 'job': 'blogger'}

It is time to encode the above dictionary object into a JSON one. To accomplish this, the Python computer programming language offers the json.dumps function.

The piece of Python code which is being shown below encodes the Python dictionary object shared above, into a JSON one.

json_data = json.dumps(d)

Once you have managed to execute the above piece of Python code in your interactive shell, print the json_data like shown below.

print(json_data)

Not only does the Python json module offer the functionality to encode objects to JSON ones, but it also gives one the ability to accomplish the reverse process. For example, to decode the object json_data into a Python dictionary, the following code should be used.

json.loads(json_data)

Let’s declare a JSON string by following the example which is being shown below.

json_string = '{"first_name": "oltjano", "lastname": "terpollari"}'

Once you have managed to declare the above JSON string in your own Python interactive console, make use of the json.loads function to convert it into a dictionary.

json.loads(json_string)

Since the JSON data interchange format is meant to be easy readable by humans, it is highly recommended that one makes use of the indent keyword argument. According to the official documentation, the indent keyword argument specifies the indentation for nested structures.

The following piece of code is an example in which the indent keyword argument is being used.

json.dumps(d, indent=4)

A real world scenario with JSON data

Web applications hosted on the internet, usually offer an API through which the developer can interact and automate certain tasks.

And most of the serious web applications on the Internet, do offer a JSON API. With the main purpose of illustrating the importance of the Python’s json module in a practical example, I am going to make use of it in a real world scenario.

There is an web application online called Fixer; it offers a simple and lightweight API for current and historical foreign exchange rates.

Make sure to signup for a free account and then grab the API endpoint with your access key. Mine looks like the one which is being shown below.

url = 'http://data.fixer.io/api/latest?access_key=97d4ae1207b5e0540f0a70aa2c839504'

Once you have managed to define the JSON API endpoint url, then make use of the import statement to include the urllib2 library in your Python’s interactive session. The code is being shown below.

import urllib2

The above library is going to help us to make requests to the JSON API endpoint url.

Once you have managed to import the urllib2 library in your Python’s interactive session then make a request to the JSON API endpoint by using the code which being shown below.

response = urllib2.urlopen(url)

Once the request has been finished, read the data by making use of the piece of code shown below.

data = response.read()

Since the data being returned in the response object is JSON, we need to decode it into a Python object with the help of the piece of code being shown below.

py_obj = json.loads(data)

Once the code has been finished executing in your own operating system, you’re going to get some output like the one which is being shown below, in your own interactive console.

{u’date’: u’2018-07-05′, u’timestamp’: 1530822548, u’base’: u’EUR’, u’rates’: {u’DZD’: 137.075029, u’NAD’: 15.827358, u’GHS’: 5.582617, u’EGP’: 20.862509, u’BGN’: 1.953452, u’PAB’: 1.169452, u’BOB’: 8.010822, u’DKK’: 7.454967, u’BWP’: 12.150576, u’LBP’: 1760.02579, u’TZS’: 2649.979182, u’VND’: 26943.015575, u’AOA’: 293.927843, u’KHR’: 4710.203677, u’MYR’: 4.725783, u’KYD’: 0.958592, u’LYD’: 1.599456, u’UAH’: 30.768293, u’JOD’: 0.828556, u’AWG’: 2.081625, u’SAR’: 4.385565, u’LTL’: 3.56531, u’HKD’: 9.174824, u’CHF’: 1.161804, u’GIP’: 0.88409, u’BYR’: 22921.268513, u’ALL’: 125.774617, u’BYN’: 2.338477, u’MRO’: 415.155899, u’HRK’: 7.397369, u’DJF’: 207.578397, u’SZL’: 15.831111, u’THB’: 38.790736, u’XAF’: 655.641853, u’BND’: 1.579814, u’ISK’: 124.839424, u’UYU’: 36.697418, u’NIO’: 36.603861, u’LAK’: 9821.061945, u’SYP’: 602.244612, u’MAD’: 11.060913, u’MZN’: 68.565, u’PHP’: 62.437066, u’ZAR’: 15.831227, u’NPR’: 128.405885, u’ZWL’: 376.978865, u’NGN’: 418.664256, u’CRC’: 660.331368, u’AED’: 4.295045, u’GBP’: 0.884621, u’MWK’: 834.334176, u’LKR’: 185.825991, u’PKR’: 142.053393, u’HUF’: 323.493938, u’BMD’: 1.169452, u’LSL’: 15.833679, u’MNT’: 2866.327976, u’AMD’: 564.202361, u’UGX’: 4489.645112, u’QAR’: 4.25657, u’XDR’: 0.830068, u’JMD’: 150.648876, u’GEL’: 2.850535, u’SHP’: 0.884131, u’AFN’: 85.72087, u’SBD’: 9.33773, u’KPW’: 1052.507078, u’TRY’: 5.367468, u’BDT’: 97.602502, u’YER’: 292.246167, u’HTG’: 73.909398, u’XOF’: 655.641853, u’MGA’: 3824.109523, u’ANG’: 2.139871, u’LRD’: 177.289002, u’RWF’: 993.368008, u’NOK’: 9.425179, u’MOP’: 9.451551, u’INR’: 80.512706, u’MXN’: 22.462959, u’CZK’: 25.933779, u’TJS’: 10.730659, u’BTC’: 0.00018, u’BTN’: 80.282915, u’COP’: 3362.643532, u’TMT’: 3.976138, u’MUR’: 40.287639, u’IDR’: 16816.726593, u’HNL’: 27.980321, u’XPF’: 119.374351, u’FJD’: 2.442943, u’ETB’: 31.832495, u’PEN’: 3.842003, u’BZD’: 2.336336, u’ILS’: 4.251079, u’DOP’: 58.05162, u’GGP’: 0.884565, u’MDL’: 19.621105, u’BSD’: 1.169452, u’SEK’: 10.250613, u’ZMK’: 10526.469865, u’JEP’: 0.884565, u’AUD’: 1.582738, u’SRD’: 8.688999, u’CUP’: 30.990491, u’CLF’: 0.027962, u’BBD’: 2.338905, u’KMF’: 492.549985, u’KRW’: 1307.120387, u’GMD’: 54.742072, u’VEF’: 134150.232509, u’IMP’: 0.884565, u’CUC’: 1.169452, u’CLP’: 767.149118, u’ZMW’: 11.378654, u’EUR’: 1, u’CDF’: 1830.77858, u’XCD’: 3.160919, u’KZT’: 401.894042, u’RUB’: 73.779352, u’XAG’: 0.072904, u’TTD’: 7.776275, u’OMR’: 0.450007, u’BRL’: 4.595832, u’MMK’: 1641.911262, u’PLN’: 4.352667, u’PYG’: 6660.03145, u’KES’: 117.58845, u’SVC’: 10.233086, u’MKD’: 61.39487, u’AZN’: 1.987487, u’TOP’: 2.708564, u’MVR’: 18.208477, u’VUV’: 127.084398, u’GNF’: 10532.089175, u’WST’: 3.060105, u’IQD’: 1384.631731, u’ERN’: 17.53006, u’BAM’: 1.959304, u’SCR’: 15.705814, u’CAD’: 1.536541, u’CVE’: 110.314454, u’KWD’: 0.353407, u’BIF’: 2046.541831, u’PGK’: 3.815456, u’SOS’: 666.587587, u’TWD’: 35.690519, u’SGD’: 1.59601, u’UZS’: 9140.441076, u’STD’: 24511.607391, u’IRR’: 50111.038572, u’CNY’: 7.758157, u’SLL’: 9554.426619, u’TND’: 3.061979, u’GYD’: 241.246343, u’NZD’: 1.722255, u’FKP’: 0.883755, u’LVL’: 0.725704, u’USD’: 1.169452, u’KGS’: 79.782382, u’ARS’: 32.789124, u’RON’: 4.663658, u’GTQ’: 8.759782, u’RSD’: 118.088503, u’BHD’: 0.443455, u’JPY’: 129.390558, u’SDG’: 20.997753, u’XAU’: 0.00093}, u’success’: True}

Final thoughts

The Python’s json module is truly important to the Python geek, especially to the web developer. As mentioned earlier in this article, most of the online web applications out there offer a JSON API through which the coder can interact with the functionalities offered by it. Personally I have written applications which interact with JSON API endpoints and I highly recommend that you keep practicing the information about the Python’s json module being shared in here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *