Serialization in Python with JSON

by Davide Mastromatteo
3 minute read


In 2016 I wrote a post about serialization in Python by using the pickle Python module.

In this article, we will try to serialize Python objects by using another module: json.

According to Wikipedia “JSON is an open-standard file format or data interchange format that uses human-readable text to transmit data objects consisting of attribute-value pairs and array data types (or any other serializable value)”.

But why you should use to use JSON instead of the official pickle module? Well, it depends on what you have to do… JSON is a safer protocol, it’s human-readable and it’s a standard adopted by a lot of languages out there (so it’s the best choice if you need a language independent platform). pickle, on the other side, is faster and allow you to serialize even custom defined objects.

So, there isn’t a silver bullet if you want to serialize and deserialize objects, you have to choose which module to use depending on your specific use case.

Now, before starting I need to tell you something: do not expect me to explain what serialization means. If you don’t know that, start by reading my previous article on this topic.

However, if you just need a hint I could also cite the 2016 myself:

To serialize an object means to transform it in a format that can be stored, to be able to deserialize it later, recreating the original object from the serialized format. To do all these operations we will use the pickle module.

Pretty straightforward, uh? And now… it’s JSON time!

I still remember the first time I met JSON… what a wonderful time! Someone told me that the JS part of the JSON word means “JavaScript” and so I had myself and decided to retire in a Buddist temple for the rest of my life.


Then I had the enlightenment and understood that I had better to decipher the whole acronym… and I found out that JSON stands for JavaScript Object Notation.

So the JavaScript part is just about the “Object Notation” token? Why did they decide to use the world Javascript on something not related to that $)%!(£& little language? Who knows…

However, I decided to dig a little bit more and this is how a JSON object appears to be:

 1{"widget": {
 2    "debug": "on",
 3    "window": {
 4        "title": "Sample Konfabulator Widget",
 5        "name": "main_window",
 6        "width": 500,
 7        "height": 500
 8    },
 9    "image": { 
10        "src": "Images/Sun.png",
11        "name": "sun1",
12        "hOffset": 250,
13        "vOffset": 250,
14        "alignment": "center"
15    },
16    "text": {
17        "data": "Click Here",
18        "size": 36,
19        "style": "bold",
20        "name": "text1",
21        "hOffset": 250,
22        "vOffset": 100,
23        "alignment": "center",
24        "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
25    }

Yes, it’s just like a Python dictionary! Isn’t that great?

So how can we serialize a Python object in a JSON format?

Quite easy, you just need to import the JSON module and then use the dumps() and loads() functions:

  • dumps() takes a Python object and returns a string with the result of the JSON serialization process.
  • loads() takes a JSON string and returns the corresponding Python object.

Let see an example of how this works:

2import json
4my_list = ["this","is","a","simple","list",35]
5my_json_object = json.dumps(my_list)
8my_second_list = json.loads(my_json_object)

Really easy, isn’t it?

It’s also possible to use the dump() and load() functions to serialize and deserialize objects. In this case, instead of strings, you will directly use files, like this:

 2import json
 4my_list = ["this","is","a","simple","list",35]
 5with open("my_file", "w") as my_file:
 6    my_json_object = json.dump(my_list, my_file)
 8with open("my_file", "r") as my_file_read:
 9    my_second_list = json.load(my_file_read)

In this last example, with the first dump() method you create a file named my_file with the JSON result of the serialization process and with the next load() method you read the previously created file and deserialize the JSON to a Python object.

Super easy, isn’t it?

Please remember that not every Python object can be serialized in JSON. This process works just with the following types:

  • dict
  • list
  • str
  • int
  • float
  • True
  • False
  • None

However, it’s possible to serialize and deserialize even custom object with just a little bit of extra magic… do you want to know how? Stay tuned for the next article! ;)

Happy Pythoning D.

Did you find this article helpful?

Buy me a coffee! Buy me a coffee!