- [Instructor] Next we're going to take a look at
serializing a Python object into JavaScript Object Notation,
and for this demonstration,
we'll be writing that object into a text file.
So first of all, we've created a Python dictionary
in this case, that we'll be writing out
as JavaScript Object Notation.
The accounts dictionary consists of a set of curly braces.
In that set of curly braces, we have one key,
which is the accounts
key, and its value is a list of other dictionaries,
and those two dictionaries represent records of information
in our accounts dictionary.
So in the accounts dictionary, the accounts key has a list
as its object, its value, and within that list
we have a dictionary that represents one account with the
account number 100, the name Jones and the balance 24.98,
and a second account, which has the value 200 for the
account number, the name Doe and the balance 345.67.
So let's now take a look at working with the JSON module,
so we're going to import that module,
and let's use a with statement to open up a file for writing
and output the dictionary
in JavaScript Object Notation format.
So as you can see here we're opening the file accounts.json.
We're using .json as the file name extension here,
but there is no specific required format or
extension for a file containing JSON objects.
We're opening for writing, so that's going to
create the file if it doesn't exist,
and otherwise it's going to wipe out
the contents of an existing file,
and we're going to reference that file object as accounts.
Now in the JSON module, there is a method,
or function, rather, called dump, which takes two arguments.
The first is the Python object that we want
to turn into JavaScript Object Notation,
and the second is the file object into which
we are going to write that information.
So we'll go ahead an execute that,
and remember that's automatically going to
close the file once we're done,
and now let's just go ahead and display the contents
of that accounts.json file so that we can see
what it looks like out on disc, if you will.
By the way again, cat, for those of you who are using
Linux and Mac OS, and you would use more
for those of you who are on Windows.
Now as you can see here,
we have just a long string of information.
JSON is just a big string of data,
and in this string of information we have the key accounts,
which was the key in our dictionary.
We have now a JavaScript Object Notation list,
and within that JavaScript Object Notation list,
we have here a JSON object.
Every JSON object is delimited by curly braces.
This JSON object has three name value pairs as part of it.
Then we have another JSON object in that list,
which also has three name value pairs as part of it.
So it looks just like the dictionary up above.
The main difference is that we have double quote characters
instead of single quote characters,
because in JavaScript Object Notation,
you must use double quote characters for strings.
In the next video, we'll take a look at
reading this JSON object back into our
iPython session.