This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 6: JSON files

JSON loading and writing exercise

face Josiah Wang

The best way to gain some experience with dealing with JSON files is to directly sink your teeth into it!

Here is a practical exercise for you to load an object from a JSON file, modify the object, and save the object to a new JSON file.

Firstly, download dataset.json.

This is a simplified subset of the annotations for the PASCAL VOC dataset packaged in the COCO dataset’s JSON format. So you are working with real Computer Vision data here (albeit completely simplified!)

Take a look at the downloaded dataset.json. You can see that it is a dictionary with two keys: "images" and "annotations". The value for "images" is a list of images (represented as a dictionary of attributes). Similarly, "annotations" gives you a list of bounding boxes (top-left x, top-left y, width, height) that indicate the position of a visual object in an image. Also notice that an image can have one or more bounding box annotations.

Task 1: Load dataset.json

Your first task is to load dataset.json into a Python dict. Try to understand the object that you have loaded, e.g. check its type, its .keys(), and try to access some of the values.

Task 2: Merge the annotations

You are interested in converting the given JSON file into a different structure. Maybe you want to visualise the dataset, so you need the bounding boxes to be part of the images.

Your aim is to produce a JSON in the following format. It should be a list, where each element in the list is a dictionary representing an image (as in the original JSON file). The dictionary should have an additional new key called “annotations”, where the value is a list of bounding boxes (represented as a dictionary with two attributes: "id" and "bbox"). Note that an image can have one or more bounding boxes.

[
  {"file_name": "007468.jpg", 
   "height": 375, 
   "width": 500, 
   "id": 7468, 
   "annotations": [
     {"id": 5776, "bbox": [2, 0, 491, 359]}
   ]
  },
  {"file_name": "002881.jpg", 
   "height": 500, 
   "width": 500, 
   "id": 2881, 
   "annotations": [
     {"id": 2202, "bbox": [393, 340, 33, 85]}, 
     {"id": 2203, "bbox": [84, 42, 239, 428]}
   ]
  },
  ...
]

Write Python code to convert the loaded dict to a list as described above.

Task 3: Save the JSON file

Your last task is easy! Save your new list that you just created to a new JSON file called visualisation.json. Feel free to also practise loading this JSON file into Python again after you have generated it!