Working with JSON Files
Contents
Working with JSON Files#
Goals of this lecture#
We’ve already discussed the basics of reading and writing text (.txt
) files.
But another common file type is a .json
file, which allows you to store structured data.
This lecture will cover:
What is a
.json
file? What is JSON more generally and why is it useful?How do we read in a
.json
file?How do we write a
.json
file?JSON files vs. JSON strings.
What is a .json
file?#
A
.json
file is a file written in the JSON file format. It allows us to store structured data objects consisting of key-value pairs.
What is JSON?#
JSON = JavaScript Object Notation.
Standard format for representing and transmitting data.
“Standard” = different people/systems agree to use this format to send and receive information.
Represents data in key-value pairs.
Check-in#
What else have we seen that represents data in key-value pairs?
A Python dict
is a collection of key-value pairs#
A dictionary (dict
) stores key-value pairs.
my_class = {'Code': '1',
'Department': 'CSS',
'Instructor': 'Trott',
'Prerequisite': True,
'Enrollment': 120}
print(my_class)
{'Code': '1', 'Department': 'CSS', 'Instructor': 'Trott', 'Prerequisite': True, 'Enrollment': 120}
my_class['Department']
'CSS'
JSON and dict
: an analogy#
Conceptually, JSON accomplishes the same goals as a Python dict
.
In fact, Python programmers often convert a
dict
into a JSONstr
when they want to store it in a file.Similarly, you can read in a
.json
file and convert the contents into adict
.
Bottom line: we’re not dealing with a fundamentally new data sturcture––it’s another standardized way to represent key-value pairs.
Reading in a .json
file#
Reading in a .json
file shares some similarities with reading .txt
files.
Must specify a file path.
File path can be either absolute or relative.
But there are also some important differences:
To read in a
.json
file, we’ll need toimport
thejson
library.json.load
will read in a structured.json
file as adict
, not astr
.
Example: simple file#
Here, we will work with a simple .json
file: data/restaurant.json
.
The file contains a structured representation of a restaurant.
We use
json.load(...)
to load this representation as adict
.
## This imports the json library
import json
## As with normal .txt. files, we use "open" to open the target restaurant
with open("data/restaurant.json", "r") as fp:
## use json.load to load as dict
info = json.load(fp)
info
{'Name': 'Plumeria', 'Location': 'University Heights', 'Cuisine': 'Thai'}
load
creates a dict
#
Now, we can work with the contents of this file as we would any dict
.
info['Name']
'Plumeria'
info['Location']
'University Heights'
info['Cuisine']
'Thai'
Check-in#
Try reading in another file that’s stored in data
: data/school.json
.
What is the value of the Name key?
### Your code here
Solution#
## As before, we use "open" to open the target file
with open("data/school.json", "r") as fp:
## use json.load to load as dict
school_info = json.load(fp)
## Get name of school
school_info['Name']
'UCSD'
Writing a .json
file#
Often, you’ll want to write a structured dict
to a file.
Useful for storing information, so you can access it later.
Useful for transmitting information between programs.
We can use json.dump(...)
to write (or “dump”) a dict
into a .json
file.
Simple example: course#
To start out, let’s use the my_class
dict we defined earlier.
my_class['Code']
'1'
To write this to a file, we:
open
(create) a file with the name we want to call it.Use
json.dump(dict_name, filename)
.
with open("course.json", "w") as fp:
json.dump(my_class, fp)
Checking that this worked#
with open("course.json", "r") as fp:
course_info = json.load(fp)
print(course_info)
{'Code': '1', 'Department': 'CSS', 'Instructor': 'Trott', 'Prerequisite': True, 'Enrollment': 120}
Check-in#
Create a new dict
called my_info
. Add the following keys/values:
Name
.Major
.
Then, use json.dump
to write this dict
to a .json
file called my_info.json
to your own computer (in whichever directory you prefer).
### Your code here
Solution#
my_info = {'Name': 'Sean', 'Major': 'Cognitive Science'}
with open("data/my_info.json", "w") as fp:
json.dump(my_info, fp)
JSON files vs. JSON strings#
The load
and dump
methods can be used to read and write a dict
from/to a .json
file.
However, Python can also represent JSON as a str
.
To read a
dict
from a JSONstr
, useloads
(load + string).To write a
dict
into a JSONstr
, usedumps
(dump + string).
json.dumps
#
Input: a
dict
.Output: a JSON
str
.
json_str = json.dumps(my_class)
json_str
'{"Code": "1", "Department": "CSS", "Instructor": "Trott", "Prerequisite": true, "Enrollment": 120}'
type(json_str)
str
json.loads
#
Input: a JSON
str
.Output: a
dict
.
Other objects besides dict
s#
Technically, you can use
dumps
/loads
for other objects, such asstr
,list
, and more.Though in my experience, a
dict
is the most common format.
json.dumps([1, 2, 3])
'[1, 2, 3]'
json.loads('[1, 2, 3]')
[1, 2, 3]
Conclusion#
This was a brief introduction to working with .json
files. Hopefully you have a better handle on:
What is a
.json
file?How do I read and write
.json
files?