This is part of a series on data storage with Python.
In this post, we'll be covering .json files. JSON stands for JavaScript Object Notation. This is the format that a lot of data is passed around on the internet, and is an extremely common way of storing information in JavaScript.
{
"author": "Jonathan Wheeler",
"email": "jonathan.m.wheeler@gmail.com",
"favorite_foods": ["sandwiches", "pizza", "anything my wife makes"],
"fake_birthday": "1992-01-23",
"pets": {
"lizard": {
"name": "Henrietta",
"address": "My wife's classroom at Mountain View Academy"
},
"squirrel": {
"name": "Sammy the Squirrel",
"address": "tree behind my house"
},
"biography": "It was a dark and stormy day. Everything seemed lost, until out of nowhere, a man appeared with a unicycle..."
}
The entire document lives inside of curly braces. Every element in the document has a key and a value "key": "value"
. If you already have experience working with dictionaries in Python, you'll notice that the syntax is pretty much identical. The only exception is that JavaScript prefers to use double quotes and Python prefers to use single quotes (sometimes, JavaScript doesn't use quotes at all around the key).
JSON documents are very similar to YAML in the types of data that they can store, and have roughly the same size as YAML documents. However, JSON documents can be a little harder for a human to edit. It's very easy to forget a trailing comma, or a quotation mark. For this reason, if a configuration file is likely to be edited by a human, YAML is the better way to go. JSON is more universally recognized by different computer languages, and so if the configuration file is likely to be edited and read by a computer and not a human, JSON is the better way to go. Furthermore, JSON is a very easy format to send data between computers via a websocket or HTTP request.
In python, you can write data into a .json file with the following code:
import json
data = {
'author': 'Jonathan Wheeler',
'date': '2021-03-22',
'array': [1,2,3]
}
with open('somefile.json', 'w') as f:
f.write(json.dumps(data))
And read it back with
with open('somefile.json') as f:
raw_contents = f.read()
data = json.loads(raw_contents)
The dumps
and loads
commands end with an s
because it is common to store json data as a byte array instead of a string. The s
indicates that we want to write/read as a string instead of a byte array.
JSON excels for passing data between computers, especially over the internet. It can also be used for configuration files. It is not a very good format for having users do a lot of editing by hand, though it isn't the worst format for this purpose.