Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
JSON is quickly becoming the de facto standard for Web-based communication. Most APIs expect, accept, and return JSON and many pieces of desktop software use JSON for configuration files. Let's see how to use Python's built-in `json` module to read and write this handy format.
Python's json
module has great documentation.
For both CSV and JSON files, and others, the amazing tablib library is a great thing to check out.
CSVs were and still are a very
popular format for importing and
0:00
exporting data to and
from databases and spreadsheets.
0:04
Much of the web though,
runs on another common and
0:08
popular format, JSON, or
JavaScript Object Notation.
0:10
No, I'm not trying to get
you to write JavaScript,
0:14
Python's JSON module is very handy for
encoding and decoding data from JSON.
0:18
You'll use this when you're
receiving information in your API or
0:23
sending information to an API, or
maybe you just want to use it to read and
0:26
write files on your server.
0:30
JSON is pretty common for
0:31
configuration files too,
regardless of how you're going to use it,
0:32
it's a great format to know about, and
a great library to know how to use.
0:36
Python's JSON library is pretty solid and
I think, pretty easy to use.
0:41
Let's check out this JSON
file about a piece of art
0:45
that I downloaded from
the Minneapolis Institute of Art.
0:49
You can see that a lot of stuff in here,
I can read it like it's a dictionary and
0:52
go, it's dated 1899, cool,
but I don't wanna do that.
0:56
I wanna be able to do this in Python,
right?
1:00
So I'm gonna make a new file and
I'm gonna call this Art.pi,
1:03
and inside of here, first things first.
1:09
We need to import the JSON module, so
1:11
the JSON module has all of
our handy dandy stuff in it.
1:14
So I'm going to open it up, what is this?
1:18
148.json as art file.
1:20
So I'm gonna open that up and
I'm gonna say art = jason.load(artfile),
1:25
and then I'm gonna
print(art['description']).
1:32
All right, cool?
1:38
So let's go run this.
1:40
And I get that it's a boat with two masts
in front of a bridge with a lift-gate,
1:45
two row boats in water at left,
buildings at left, trees at right,
1:49
that's the description I
got from the JSON file.
1:52
Now that might not look like much but it's
really handy if you're dealing with tons
1:55
of input from an outside source, or
lots and lots and lots of files.
1:58
And you don't have to try and
turn plain-text files in Python Objects or
2:02
anything like that through RegX or
some sort of, I don't even know,
2:08
some sort of arcane incantation.
2:12
The load method, which we used right here,
takes a file-like object and
2:15
tries to turn that into
a Python object of some kind.
2:21
In this case, because what we have here is
what we would call a dictionary in Python,
2:26
it just creates a dictionary,
it creates an actual Python dictionary.
2:31
If we had a list, an array for
JavaScript, then we would get a list
2:35
in Python, another way that we can
do this is with the load s method.
2:41
You'll probably hear people call
it loads instead of load s or
2:45
load string, I call it loads all the time.
2:48
Honestly, that's fine, that's what the
word is, but the way that it's spelled,
2:52
what it is, is it's the way that we can
take a string with embedded JSON in it and
2:56
turn that into a Python object.
3:01
Let's try that out,
let's try it out down here in the shell.
3:04
We'll do Python, import json and
3:06
let's do nums = json.loads and
3:10
then a string of 1,2, and 3.
3:14
All right, and so
now if I do nums[2] I get the number 3,
3:18
so we loaded the string in the JSON.
3:24
In that string there was an array of
integers which the JSON module turned
3:27
into a list of ints and
I think that's pretty handy.
3:31
The opposite of load and
loads are the dump and
3:36
dumps methods, so let's try them out.
3:41
So we can do json.dumps([5, 4, 3, 2, 1]).
3:45
No quote marks, and now, I get a string
which has the array of numbers in it,
3:52
so it's kinda like I turned
the list into a string,
3:57
I mean, it almost seems like I get
to str and then the list, right?
4:00
And I could on something this simple,
but on something that's a little bit
4:05
more complicated, you obviously
can't do that, let's try this.
4:08
I'm gonna make myself into a dictionary,
so first_name is Kenenth,
4:11
last_name is Love, and
my topic here at Treehouse Is Python.
4:17
All right, so I can't just do str(me),
4:24
I mean, I can but I don't get
exactly the right thing, right?
4:28
So let's do json.dumps(me) and
4:33
while it looks the same,
there is a difference because in JSON
4:37
you have to have double quotes instead
of single quotes to make it valid.
4:40
So this is valid JSON, what I did
before is not, and if I was to have
4:45
numbers like that in there, we'd want
to change how those things worked.
4:49
We could also easily send this string out
across the Internet with no problems, so
4:52
that's great,
4:57
that's super handy, we could do something
similarly using the Dump method.
4:57
But Dump goes directly into a file-like
object instead of into a string,
5:02
so we have me, let's make Craig.
5:08
So again, do first_name as Craig,
5:11
last_name as Dennis, and
topic as Java all right?
5:16
So we have me, and we have Craig,
and let's do with open
5:24
teachers.json, and a for append,
although it doesn't really matter,
5:28
cuz we're writing into this for
the first time, as teacherfile.
5:34
Then we wanna say json.dump, and
5:39
we're gonna dump an array of me and
5:43
craig into teacherfile, all right?
5:47
Let that run and now,
let's shrink that down, refresh this,
5:52
if we checked out teachers.json
that the file that we just created.
6:02
There's me, and
I misspelled my own name, and
6:08
there's Craig, and
it made a list of objects.
6:12
So that's pretty cool, that's super
awesome, the JSON module is amazingly
6:17
handy for both encoding and decoding
data to and from files and strings.
6:22
I find myself using it a lot with
various APIs on the web, and
6:27
I bet that you will find it the same.
6:30
You need to sign up for Treehouse in order to download course files.
Sign up