JSON with Python – Reading & Writing (With Examples)

Modern software development is very much dependent on storing and exchanging data. The last two decades have seen many ways of storing and transferring data. Today, One of the efficient and accessible ways is using JSON with Python. 

JSON stands for Javascript Object Notation. One another popular way is using XML. It isn’t easy to parse, read, and write in XML when compared to JSON. The syntax of JSON comes from Javascript object literals. But JSON is in the text format. You can generate JSON through any programming language and quickly parse it. 

As mentioned above, JSON is javascript objects in text format.

Observe the following code:


{

    name : "John",
    age : 25,
    location : "New York"

}

The following is the JSON format of the above data:

{

    "name" : "John",
    "age" : 25,
    "location" : "New York"

}

You can see both are very similar. In the JSON, we write keys inside quotes. JSON files uses the .json extension. You have to store the JSON file with the .json extension. 

The most common use of JSON is exchanging data between a REST API client and a web server.

Video tutorials on how to use Python for JSON, XML, etc. are also available from LinkedIn Learning.

Check out this course:

JSON with Python

JSON data is a string. Let’s say you want to convert a Python dictionary. To do that, you have to encode the dictionary as a series of bytes.

This process is called serialization. Similarly, the reverse of serialization is deserialization. It means, decoding JSON into a python object. Let’s discuss serialization and deserialization in Python using the in-built methods.

Python also comes with the JSON module. 

You may like:

To work with JSON in Python, you need to import the JSON module in your python file. You can import the JSON module using the following statement:

import json

Serialization

Serialization means converting a Python data type into JSON. The JSON encoder only supports data types that are native to Python. These are int, str, bool, float, list, dict, and tuple. You can convert the native data types into JSON.

Read: Data Structures & Algorithms in Python – Introduction to Hash Tables

The JSON module has two methods for serialization – dumps() and dump().

dumps()

The dumps() method takes a Python object as a parameter and returns a JSON string. Here’s an example:

import json

dc = { "name" : "John", "age" : 25, "location" : "New York"}
js = json.dumps(dc)
print(js)

In the above code, I created the dictionary dc, and serialized it using dumps() method using JSON module. Let’s see what is the output:

json with python

It returns a JSON object. You may say that a Python dictionary and the returned JSON looks similar.

Let’s try to convert another data type into JSON.

import json

tp = (1,2,3,4,5)
js = json.dumps(tp)
print(js)

This time, instead of dictionary, I am using a tuple.

Output

json with python

Our code converts the tuple into a JSON array.

There are two additional parameters that you can use with the dumps() method – sort_keys and indent.

Setting the sort_keys to True will sort key-value pairs in the result. We get our results in ascending order.

The indent parameter will customize the indentation of our output according to the value we specify.

Both of these parameters make the result easily readable.

import json

dc = { "name" : "John", "age" : 25, "location" : "New York"}
js = json.dumps(dc, sort_keys = True, indent = 3)
print(js)

The sort_keys is set True and the indent is 3. Let’s check out the output:

json with python

dump()

The dump() method also converts a python object into JSON, but it stores the resultant data in a file. Observe the following code:

import json

dc = { "name" : "John", "age" : 25, "location" : "New York"}
file = open("newfile.json", "w")
json.dump(dc, file, sort_keys = True, indent = 3)
file.close()

The name of the file is newfile.json, in which we will store the data.

You can see that we are passing the name of the file as the second parameter.

Please have a look at the newfile.json file after we ran the code:

json with python

The converted dictionary is present in our JSON file.

De-serialization

De-serialization is the reverse process of serialization. It converts encoded JSON data into a Python native data type.

The JSON module has two methods for de-serialization – loads() and load().

loads()

The loads() method takes JSON data as the first parameter and returns a python native object. It is the reverse of the method dumps().

import json

js = '{ "name" : "John", "age" : 25, "location" : "New York"}'
dc = json.loads(js)
print(dc)

I created a JSON object js. Then passed it to the loads() method of the JSON module. Note that JSON data is a string.

And here is our output:

json with python

load()

Now let’s come to the method load().

The load() method is to read JSON from a file and convert it into a native python data type. First, read the file and then pass it to the load() method.

import json

file = open("newfile.json", "r")
data = json.load(file)
print(data)

file.close()

After reading the newfile.json, it is passed to the load() method. The variable data stores the converted value. 

json with python

Custom Objects

Until now, we discussed JSON with Python using in-built Python methods.

No doubt, these methods are useful, but in a real-time application, you may need to deal with manually created class-based objects. You can also perform serialization and de-serialization on custom objects.

Observe the following python code:

import json

class DemoClass:
    def __init__(self, val1, val2):
        self.val1 = val1
        self.val2 = val2

obj1 = DemoClass("Value1", "Value2")

We have a class, DemoClassThe __init__() method is initializing two attributes val1 and val2

Then, we create an instance of DemoClassobj1

Let’s serialize this object using the dumps() method and see what we get.

import json

class DemoClass:
    def __init__(self, val1, val2):
        self.val1 = val1
        self.val2 = val2

obj1 = DemoClass("Value1", "Value2")
js = json.dumps(obj1)

print(js)

Output:

json with python

It throws an error:

TypeError: Object of type DemoClass is not JSON serializable

According to this error, you can not serialize an object of a manually created class.

To solve this problem, Python has a __dict__ attribute. Let’s pass it along with our object to the dumps() method.

import json

class DemoClass:
    def __init__(self, val1, val2):
        self.val1 = val1
        self.val2 = val2

obj1 = DemoClass("Value1", "Value2")
js = json.dumps(obj1,__dict__)

print(js)

Let’s see if it works now:

json with python

It works perfectly.

Conclusion

In modern software development, JSON plays a crucial part.

Throughout this article, you may have noticed how I stored data in JSON and made conversions.

Python has handy in-built methods for such serializations and de-serializations. That’s one of the reasons why I love to work JSON with Python so much.

To learn more on how to use JSON with Python or JSON in general, then check out these courses from LinkedIn Learning:

Also, remember that you can sign up for a LinkedIn Learning account for absolutely free.

Click here for a free account.

Do you love to handle JSON with Python? What are your thoughts on this article? Do you have any questions?

Leave a Reply