Working with the JSON library in Python

The JSON (Java Script Object Notation) is defined as a light-weight, data interchange file format. The syntax is specified by The IETF Datatracker and the ECMA. These are organizations dedicated to the standardization of databases and communication systems. This makes working with the JSON library in Python easier and more intuitive than ever.

JSON is a format that is used to send, receive and store data from systems in a network. It is “light-weight” in comparison to larger formats, such as HTML or XML. There are formatting techniques in Python that can be used to convert JSON to Python objects and vice versa. Python is built with a standard library of helpful modules and the JSON module is one of them. Once we import it using the following code, we have full use of its functionalities.

import json

JSON functions: json.dumps()

Syntax for json.dumps():

json.dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

Some parameters used:

  • obj is the Python serializable object that you want to convert into a JSON format.
  • fp is a file pointer used to write JSON formatted data into a file. 
  • skipkeys is default = False. If coded as True, then any dictionary keys that are not of a basic type (i.e. str, int, float, bool, None) will be skipped instead of raising a TypeError
  • ensure_ascii is default = True and the output will have all incoming non-ASCII characters escaped. Changing that to false, the characters will be the same on output as on input.
  • allow_nan is default = True. The JavaScript equivalents such as NaN, Infinity and -Infinity will be used. When marked as False, if we attempt to serialize out of range float values, we will get a ValueError
  • indent is used to make the JSON output more readable. It prints it in pretty-print format.
  • sort_keys is default = False. When it is marked as True, the output of dictionaries will be sorted by key

The json.dumps() method is used to convert a Python object to a JSON string. Let’s say we receive an HTTP request from an application to send over the details of a popular drag queen from RuPaul’s Drag Race. The data is stored in a database. We can retrieve the information and store it in a Python dictionary. Then we can convert the Python dictionary into a JSON formatted string to send as a response to the request. To do this, we can use the json.dumps() method.

import json
 
queen = {'Name': 'LaGanja Estranja', 'Drag Race Season': 6, 'fav_color': 'green', 'skills': ['death drops', 'performance', 'green goddess']}
 
new_json_object = json.dumps(queen)
 
print(type(new_json_object))
print(new_json_object)

Output:

<class 'str'>
{'Name': 'LaGanja Estranja', 'Drag Race Season': 6, 'fav_color': 'green', 'skills': ['death drops', 'performance', 'green goddess']}

The output is a converted Python object that is now a JSON string.

JSON functions: json.dump()

Syntax for json.dump():

json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

The json.dump() method is used to write a Python serialized object into a JSON formatted data file. The parameters are similar to the json.dumps() method above.Using the same example from above, we can take the returned JSON string and store it in a file for future use. Using the json.dump() method, we can convert the Python dictionary into a JSON format and write it into the file.

import json

queen = {'Name': 'LaGanja Estranja', 'Drag Race Season': 6, 'fav_color': 'green', 'skills': ['death drops', 'performance', 'green goddess']}

with open("queen.json", "w") as write_file:
  json.dump(queen, write_file)

After running the above code, we have created a JSON formatted data file called “queen.json”

Mapping during encoding and decoding

Mapping happens between JSON and Python entities while encoding JSON data. Encoding is also called serialization. Deserialization is the process of decoding the data. These are the transformations that process our data into a series of bytes that are then stored and/or transmitted across a network. 
To encode Python objects into JSON equivalent, the Python json module uses a specific conversion. The json.dump() and json.dumps() methods both perform the conversions when encoding. The following is a conversion table from the official Python documentation. It displays the intuitive mapping between JSON and Python data types.

JSON functions: json.load()

Syntax for json.load():

json.load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)

Some parameters used:

  • fp is a file pointer used to write JSON formatted data into a file. 
  • object_hook is the optional function that will be called with the result of any object literal that is decoded. It is a custom decoder that can convert JSON data types into Python types other than the primitive ones it is built to handle. 
  • object_pairs_hook is similar to the optional function above but in regard to object literals decoded with an ordered list of pairs.

JSON files are read using the json.load() method in Python. Another way to say this is that the load() method is used to deserialize a file to a Python object. Using the same example as above, we now have a separate JSON file named “queen.json” that contains the following data:

{'Name': 'LaGanja Estranja', 'Drag Race Season': 6, 'fav_color': 'green', 'skills': ['death drops', 'performance', 'green goddess']}

We can open and read the data file we created by using the json.load() function.

import json

with open("queen.json", "r") as read_file:
    queen_info = json.load(read_file)
for key, value in queen_info.items():
        print(key, ":", value)

Output:

Name : LaGanja Estranja
Drag Race Season : 6
fav_color : green
skills : ['death drops', 'performance', 'green goddess']

The output is the Python Dictionary converted from the JSON data file. 

JSON functions: json.loads()

The json.loads() method  is used to convert a JSON string into a Python dictionary. It will deserialize native string, byte, or byte array instances containing JSON data. This method as well as the json.load() method both use the same mapping conversion table mentioned earlier.

import json
 
queen = """{"Name": "LaGanja Estranja", "Drag Race Season": 6, "fav_color": "green", "skills": ["death drops", "performance", "green goddess"]}"""

print(f"the queen variable is of the type:{type(queen)}")
 
queenDictionary = json.loads(queen)
print(f"the queenDictionary variable is of the type:{type(queenDictionary)}")
print(queenDictionary)

Output:

the queen variable is of the type:<class 'str'>
the queenDictionary variable is of the type:<class 'dict'>
{'Name': 'LaGanja Estranja', 'Drag Race Season': 6, 'fav_color': 'green', 'skills': ['death drops', 'performance', 'green goddess']}

This returns a Python dictionary from the JSON string. We can also find specific data from the dictionary by accessing its keys directly.

queenDictionary = json.loads(queen)
 
print(queenDictionary["Name"])
print(queenDictionary["fav_color"])
print(queenDictionary["skills"])

Output:

LaGanja Estranja
green
['death drops', 'performance', 'green goddess']

Summary

This has been a tutorial on working with JSON data in Python. We covered a few of the functions and their implementations.  If you enjoyed this article, please follow me, Z. Myricks here on PythonAlgos for more guides in all things Python and Natural language Processing. Connect with me on Twitter and LinkedIn also!

Further Reading

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: