Serialisation with JSON

OK, so imagine we have a simple student records system that uses a dictionary for storing the data. What if we would like to save this so that at a later date we can load it?

For example, we might have the following data in a dictionary.

std_dict = {
    "100456789": ["Sam O'Neill", "Computer Science"],
    "100123456": ["Roisin Hunt", "Computer Games `Modelling and Animation"],
    "100654321": ["Tom Hughes-Roberts", "Computer Games Programming"],
    "100987654": ["Bradley Davis", "Computer Science"],
    "100345678": ["Rich Conniss", "Mathematics"],
    "100876543": ["Patrick Merritt", "Computer Games Programming"]
}

1. Saving and Reading a Dictionary with Standard Write

We could try and do this by saving the dictionary to a txt file, for example:

std_dict = {
    "100456789": ["Sam O'Neill", "Computer Science"],
    "100123456": ["Roisin Hunt", "Computer Games `Modelling and Animation"],
    "100654321": ["Tom Hughes-Roberts", "Computer Games Programming"],
    "100987654": ["Bradley Davis", "Computer Science"],
    "100345678": ["Rich Conniss", "Mathematics"],
    "100876543": ["Patrick Merritt", "Computer Games Programming"]
}

# save the dictionary to the file std.txt
with open('std.txt', 'w') as file:
  for (k,v) in std_dict.items():
    file.write(f"{k}:{v}\n")

This will save the following in std.txt

100456789:["Sam O'Neill", 'Computer Science']
100123456:['Roisin Hunt', 'Computer Games `Modelling and Animation']
100654321:['Tom Hughes-Roberts', 'Computer Games Programming']
100987654:['Bradley Davis', 'Computer Science']
100345678:['Rich Conniss', 'Mathematics']
100876543:['Patrick Merritt', 'Computer Games Programming']

We could then attempt to load this back in using something like:

# try and load the dictionary back into memory from std.txt
load_dict = {}

with open('std.txt', 'r') as file:
  for line in file.readlines():
    item = line.split(":")
    load_dict[item[0]] = item[1]

print(load_dict)

This prints out:

{'100456789': '["Sam O\'Neill", \'Computer Science\']\n', '100123456': "['Roisin Hunt', 'Computer Games `Modelling and Animation']\n", '100654321': "['Tom Hughes-Roberts', 'Computer Games Programming']\n", '100987654': "['Bradley Davis', 'Computer Science']\n", '100345678': "['Rich Conniss', 'Mathematics']\n", '100876543': "['Patrick Merritt', 'Computer Games Programming']\n"}

This is not quite what we had in mind, in fact, we would need to make some alterations to the code to get it to be the original dictionary.

But it gets worse, what if we have a different data structure, maybe we have a dictionary of dictionaries etc? In this case, we would now need to modify the code again!

This leads us to use serialisation which allows us to convert a python object into something we can then store and then at a later date just reverse the process.

We can serialise using many formats, JSON, pickle, XML etc. We will just look at JSON. In general, this is used for transmitting data in web applications.

If you were building a real student record system you would store the data in a database.

2. Saving and Reading a Dictionary with JSON Serialisation

Here is how we can do this in JSON though.

We can first save the dictionary as JSON in the file std_dict.json, we will also indent the file to make it more readable.

import json

std_dict = {
    "100456789": ["Sam O'Neill", "Computer Science"],
    "100123456": ["Roisin Hunt", "Computer Games `Modelling and Animation"],
    "100654321": ["Tom Hughes-Roberts", "Computer Games Programming"],
    "100987654": ["Bradley Davis", "Computer Science"],
    "100345678": ["Rich Conniss", "Mathematics"],
    "100876543": ["Patrick Merritt", "Computer Games Programming"]
}

with open('std_dict.json', 'w') as f:
    json.dump(std_dict, f, indent=4)

This will output the following to std_dict.json

{
    "100456789": [
        "Sam O'Neill",
        "Computer Science"
    ],
    "100123456": [
        "Roisin Hunt",
        "Computer Games `Modelling and Animation"
    ],
    "100654321": [
        "Tom Hughes-Roberts",
        "Computer Games Programming"
    ],
    "100987654": [
        "Bradley Davis",
        "Computer Science"
    ],
    "100345678": [
        "Rich Conniss",
        "Mathematics"
    ],
    "100876543": [
        "Patrick Merritt",
        "Computer Games Programming"
    ]
}

We can then load it back into memory.

import json

with open('std_dict.json', 'r') as f:
    new_dict = json.load(f)

print(new_dict)

Which prints out:

{'100456789': ["Sam O'Neill", 'Computer Science'], '100123456': ['Roisin Hunt', 'Computer Games `Modelling and Animation'], '100654321': ['Tom Hughes-Roberts', 'Computer Games Programming'], '100987654': ['Bradley Davis', 'Computer Science'], '100345678': ['Rich Conniss', 'Mathematics'], '100876543': ['Patrick Merritt', 'Computer Games Programming']}

Much better!

=== TASK ===

This should be a nice easy one.

Create a load() function that takes in a filename and loads the data stored in the filename.

Copy and paste the following into a new Python file to get started.

def load(filename):
  pass

if __name__ == "__main__":
  print(load("items.json"))

items.json data from W3Schools