The goal is to take our password manager to the next level.
And in order to do that, we need to add a search functionality.
It's not good enough just to look through the data file anymore.
What we want to be able to do is to type a website into the website entry, and then hit search, and
we get a pop up showing us our email and the password that was saved for that website.
So this is the goal.
But where we're currently at is we have a text file which contains the name of the website, the email
and the password.
This is very difficult to search and this is a really terrible format to work with.
So we're going to be levelling up our data storage in our password manager.
And we're going to switch from saving data straight to a text file to a fancier data format, which
is called JSON.
Nope.
This is not the latest, hottest DJ on the block.
Lock.
It's actually JSON, which stands for JavaScript Object Notation.
This was something that was designed originally for JavaScript, but because it has such a simple structure
and it's so easy to understand and work with that it's been adopted by many, many different fields,
including Python.
This is probably one of the most popular ways of transferring data, especially when you're transferring
data across the internet.
So this is what we're going to be learning about.
And for those of you looking at this slide, you might have already realized that it's kind of similar
to the dictionaries that we've been working with in Python.
Right?
A JSON is essentially composed of a bunch of nested lists and dictionaries, and it has that key value
pair data structure.
This is essentially what we're aiming for, so that we can store our data in this format.
And we'll be able to easily load up this data and search through it for the particular website that
we want the information for.
To work with JSON data in Python, we can use the inbuilt JSON library, and we're going to use it to
write, read and update data to a JSON file.
To begin, I'm going to simplify our code a little bit.
I'm going to get rid of this message box which asks the user okay or cancel.
We're just going to assume that once they click add, then they're happy with the email and password.
So now that we've cut down that little bit of code, it makes this a little bit easier to understand.
All that we're doing here is we're checking to make sure that the length of the website entry and the
length of the password entry is equal to zero.
And you might still have the length checking for the email entry.
But remember that at the end of previous lessons, we actually inserted a default value so we can actually
skip that check as well if you wanted to.
The reason why we want all code as simple as possible is so that when we're working with the JSON and
we're learning about how to work with it, as long as everything else is simple, then we can focus
on the new things that we're learning.
In this case, instead of opening a data dot txt file, I'm going to change the data format to a dot
JSON and instead of using the append mode, I'm going to use the write mode because I'm going to be
writing to this data dot JSON file.
Now, in order to write to a JSON file, we don't use this line of code.
Instead we use a method called JSON dot dump.
And that of course requires us to import the JSON module, which should be inbuilt to Python so you
don't actually have to install it.
Now, once we've imported our JSON module we can say JSON dot dump.
And this takes a number of inputs.
But the most important are the things that you want to dump and the file that you want to dump it to.
The data that we want to put in here should go in as a dictionary.
Essentially, what we want to create is a new dictionary, which I'll call new data.
And it's going to be a nested dictionary.
So the first level key is going to be the website, because this is what we're going to be searching
through eventually.
And the website is going to itself contain a dictionary.
Now this dictionary contains two keys the email and also the password.
The values for each of these are pretty self-explanatory.
It's the email that we got from this line of code, and the password is the password that we got from
this line of code.
Now that we've created this new dictionary called New Data, well, that is what we're going to use
to dump into our JSON file.
Let's go ahead and dump our new data.
So that's the first input.
Now the next input is going to be the data file that we want to put it into.
So that is going to be the file that we opened up inside this line.
Let's provide our data file as the location to dump this data.
And now if I go ahead and hit run and I create a new entry for my Amazon website password, generate
the password, hit add.
Then you can see that we've got our brand new data JSON file that's just been created.
So just as a quick reminder, when you open a file in write mode, if that file doesn't exist, it will
actually create it.
So we've created this new file data dot JSON.
And we've dumped the data that we entered into that file.
So now if I open this up you can see we've got a JSON format in here.
And we've got our Amazon website and all of its associated data.
Notice how this data is not very easy to read for a human.
So we can actually improve that by adding one other argument.
And it's an argument called indent.
This we can provide the number of spaces to indent all the JSON data so that it becomes much easier
to read.
So I'm going to go ahead and delete everything that's in here.
And I'm going to run the code again.
And this time once I save this data into my data dot JSON, you can see everything is all indented and
it's much easier for the human to read.
Now that we've seen how we can write data to a JSON file, the next step I want to show you is how you
can load data from the JSON file, or how to read from it.
To read JSON data, we use the JSON module and we call the load method.
So I'm going to go ahead and comment out this line of code.
And now that I've actually got some data in my JSON data file, I'm going to try and read from it.
We call the JSON dot load method and we pass in the file path.
Or you can see here the PHP.
So that's the only required input.
The file that we're going to pass in is our data file which we opened up over here.
And what we're going to do is we're going to change this from write mode to read mode.
Once we've loaded up the data we're going to save it inside a variable called data.
And then I'm just going to print it out.
So when I hit run right now and I simply add any sort of gobbledygook.
But as long as I can hit the add button, it's going to trigger this part of the code.
And now if we take a look inside our console, you can see that data is being printed down here, but
without any of these indentations or any of the formatting.
Instead, what that load method does is it essentially takes this JSON data and converts it into a Python
dictionary.
So this data, if we actually do a type check on it, you can see has a type of dictionary.
This is just a normal Python dictionary.
So essentially we can use Json.dumps and Json.load to serialize and deserialize from JSON data to Python
dictionaries.
And it allows us that free interchange of information.
We change it into a JSON to store it, and then we take it out of storage and turn it into a Python
dictionary to easily work with it in our code.
Now that we've seen how to write, how to read, the last thing I want to show you is how to update
data.
Because if we have a new piece of data come in, we want to add to this JSON, but we don't want to
overwrite it.
But we also don't want to just append to the end of it.
Because you can imagine if we had something that we just appended to the end.
That's not a valid data structure.
And that's why we're getting all of these errors.
Instead of doing it manually, we're going to use the built in method JSON dot update.
And here's how it works.
So we've already got some data inside our data dot JSON file.
And what we want to do is we want to take that data and we want to update what's existing in there with
the new data that's being passed in.
To do that, we first have to load up the data.
So I'm going to uncomment this line.
And once we've gotten hold of the data we're going to say data dot update.
And we update it with the new data.
What's happened so far is we have our data loaded up into a dictionary.
We use the update method to update that dictionary with some new piece of data.
Now, the next thing we want to do is we want to actually write that data back into this file.
So we have to use this line of code using JSON dump.
But instead of dumping the new data we're going to dump that data that we updated right here.
Essentially what we're doing here is a three step approach.
We're reading the old data.
We're updating old data with new data.
And we're finally saving the updated data.
Here we've got two things happening.
We've got reading and we've got writing.
So we can split this code into two sections.
First we open the file in read mode.
We get hold of the data, we update the data, and then we open up the file again.
But in this case we open it in write mode.
This time we're going to dump the data that we've updated over here into that data file using this particular
indent format.
And once we've written to file and we delete all the text from our website and password entries, and
these lines of code now replace our previous functionality where we were saving just plain text to our
data dot text.
Let's go ahead and run it and test it out.
Currently we have a single entry inside our data dot JSON.
So if we go ahead and add a new entry say for eBay and we generate password, click add.
If we go back to our data dot JSON, you can see how it's now been updated.
This entire JSON has now been updated to have two entries Amazon and eBay.
It didn't just appended it actually added it into the dictionary.
And that's all thanks to that update method.
and once it updated that data, then we told it to save the updated data back into the data file and
wipe all of the previous data.
So now that we're able to read, write, and update JSON data, the next step is to think about what
situations might this fail?
And one of the biggest problems we have at the moment is we're trying to open up this file, load up
the data and update it.
But if we actually had no data in here, say the first time we ran our program, or indeed if we didn't
actually have this data dot JSON file, then you can see that the first time I run it and I try to save
a password.
Then it's going to crash.
And we get some exceptions being thrown.
But we know all about exceptions now.
So in the next lesson that's what we're going to tackle for all of that and more.
Head over to the next lesson.