How to Perform Read and Write Operations on JSON Files in Python

A simple guide to handle JSON files in Python.

Andreas Soularidis
Python in Plain English

--

Photo by S Migaj on Unsplash

Hi everyone, in my last article we talked about text files and we learned how to perform read and write operations. Even if text files in some cases are a great way to store data permanently, they have some important drawbacks. One of them is that we cannot store objects, or the data are not stored in a structured way. These disadvantages came to cover the JSON standard. In the rest of the article, we will demonstrate how to read and write data using JSON files, we will talk about serialization and deserialization and finally, we will build a mini-project. We have a lot of stuff to do, so let’s start.

Description

JSON (comes from JavaScript Object Notation) has quickly become the standard way not only for data storing but also as a way for data exchanging via the internet. Most APIs use JSON to deliver the data. The most important reason for the widespread acceptance of the JSON standard is that it is easily understood by humans and machines. A JSON file has the following form:

{
"first_name" : "John",
"last_name" : "Doe",
"semester" : 2,
"curent_courses" : ["Algorithms", "Data Structures", "Databases"]
}

As we can see in the example above, a JSON file looks like a dictionary in Python and supports primitive data types like strings, integers, lists.

To read and write data to a JSON file, we have to open it like the text files we talked about in the last article. Thus, we can use them with open(filename, mode) block to read and write files.

Disclaimer: In this article, we suppose that the files are in the same directory, so we do not need to specify the path to the file.

Write Data to File

As we mentioned earlier in this article, to add new data to a file, we must open it, using the open() method. To store our data in a JSON file, we can use the dump(object, file) method. This method converts (serializes) a Python object into a JSON string. With the term serialization, we mean the transformation of data into a series of bytes to be stored or to be transmitted via the web. This process converts Python objects into JSON equivalents as follows:

Python      ->   JSON
dict Object
list, tuple Array
str String
int, float Number
True true
False false
None null

The dump() method takes two parameters, the Python object we need to store and the filename. In the following example, we have a dictionary with the data of a student and we try to store them in a JSON file. Let’s see the code.

If we execute the code above, we create a new JSON file that contains the stored data.

Read Data from Files

To read data from a JSON file, we can use the load(filename) method. This method takes as a parameter the filename and converts (deserializes) the JSON data to Python objects. The deserialization process is the reverse of the serialization process and it is performed as follows:

JSON     ->   Python        
Object dict
Array list, tuple
String str
Number int, float
true True
false False
null None

Now, let's read the data of the student from the previous example.

In the example above, we use the load() method to read the data from the file, and then we can handle the data as a dictionary.

Mini Project

Suppose we want to create a simple app that takes the data of students and stores them in a file. The user can insert the data into the app, store them, and retrieve them. I recommend you give it a try on your own before reading my solution.

Conclusion

In this article, we talked about JSON files in Python. The JSON standard has a great impact on the way we store and exchange data on the web. In the next article, we will talk about basic rules that we need to follow whenever coding to have “clean ”code. Until then, keep coding and keep learning. Thanks for reading!

More content at plainenglish.io. Sign up for our free weekly newsletter here.

--

--

Hi, my name is Andreas and I'm passionate about Programming, Algorithms, Machine Learning, Semantic Web, and IoT.