You are on page 1of 2

Python Programming home

Department of Computing, Imperial College London

Python for C++ Programmers


Chapter 1: Introduction
Chapter 2: Basic data types
Chapter 3: Variables and operators
Chapter 4: Sequence types
Chapter 5: Sets and dictionaries
Chapter 6: Control flow
Chapter 7: Functions
Chapter 8: Object-oriented programming
Chapter 9: Modules
Chapter 10: Files
[10.1] Handling text files
[10.2] JSON files
[10.3] Loading JSON files
[10.4] Writing to JSON files
[10.5] pickle
[10.6] Pickling time!
[10.7] CSV files
[10.8] Reading CSV files
[10.9] Reading CSV files into a dict
[10.10] Writing to CSV files
[10.11] That's a wrap!

Chapter 10: Files


>> JSON files
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.

Previous Next 

Page designed by Josiah Wang Department of Computing | Imperial College London

You might also like