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

face Josiah Wang

You will likely see many Machine Learning datasets nowadays representing their data in JavaScript Object Notation (JSON) format. One example would be the COCO dataset which stores its annotations in JSON.

JSON is also used quite often for communication between a web server and your web app or browser. That tweet or Facebook Timeline post that you just received? Possibly sent using JSON in the background (I have not verified this though, so I might be completely wrong!)

If you look at an example JSON file (below), it may look awfully familiar. What does it remind you of? A dict perhaps?

{
    "name": "Smith", 
    "interests": ["maths", "programming"], 
    "age": 25, 
    "courses": [ 
        {
            "name": "Python", 
            "term": 1
        }, 
        {
            "name": "Soft Eng", 
            "term": 2
        } 
    ] 
}

The root JSON object is generally either a list (called array in JSON terms) or a dictionary (called object in JSON terms).

json module

In Python, the json module can be used to load from and write to a JSON file. Note that JSON is not an exact one-to-one mapping to Python types though. For example, true is lowercase, strings can only use double quotes, dictionaries are considered objects, None is called null, etc. The json module will, however, automatically map between Python and JSON types in the background, so you do not have to worry too much about the details as long as you only use basic Python data types.