WEBVTT

00:00.200 --> 00:04.560
The goal is to take our password manager to the next level.

00:05.000 --> 00:09.160
And in order to do that, we need to add a search functionality.

00:09.200 --> 00:13.000
It's not good enough just to look through the data file anymore.

00:13.400 --> 00:19.600
What we want to be able to do is to type a website into the website entry, and then hit search, and

00:19.600 --> 00:25.400
we get a pop up showing us our email and the password that was saved for that website.

00:25.760 --> 00:27.120
So this is the goal.

00:27.960 --> 00:35.880
But where we're currently at is we have a text file which contains the name of the website, the email

00:35.880 --> 00:37.000
and the password.

00:37.200 --> 00:42.920
This is very difficult to search and this is a really terrible format to work with.

00:43.320 --> 00:48.320
So we're going to be levelling up our data storage in our password manager.

00:48.320 --> 00:55.320
And we're going to switch from saving data straight to a text file to a fancier data format, which

00:55.320 --> 00:56.800
is called JSON.

00:57.360 --> 00:57.720
Nope.

00:57.720 --> 01:01.560
This is not the latest, hottest DJ on the block.

01:01.560 --> 01:01.690
Lock.

01:01.890 --> 01:07.050
It's actually JSON, which stands for JavaScript Object Notation.

01:07.610 --> 01:14.410
This was something that was designed originally for JavaScript, but because it has such a simple structure

01:14.410 --> 01:19.650
and it's so easy to understand and work with that it's been adopted by many, many different fields,

01:19.650 --> 01:20.970
including Python.

01:21.410 --> 01:26.930
This is probably one of the most popular ways of transferring data, especially when you're transferring

01:26.930 --> 01:28.650
data across the internet.

01:29.010 --> 01:31.170
So this is what we're going to be learning about.

01:31.170 --> 01:36.730
And for those of you looking at this slide, you might have already realized that it's kind of similar

01:36.730 --> 01:39.730
to the dictionaries that we've been working with in Python.

01:39.770 --> 01:40.210
Right?

01:40.810 --> 01:49.250
A JSON is essentially composed of a bunch of nested lists and dictionaries, and it has that key value

01:49.250 --> 01:50.730
pair data structure.

01:51.090 --> 01:57.330
This is essentially what we're aiming for, so that we can store our data in this format.

01:57.330 --> 02:04.230
And we'll be able to easily load up this data and search through it for the particular website that

02:04.230 --> 02:05.910
we want the information for.

02:06.630 --> 02:14.350
To work with JSON data in Python, we can use the inbuilt JSON library, and we're going to use it to

02:14.390 --> 02:18.630
write, read and update data to a JSON file.

02:19.070 --> 02:22.150
To begin, I'm going to simplify our code a little bit.

02:22.150 --> 02:27.510
I'm going to get rid of this message box which asks the user okay or cancel.

02:27.710 --> 02:33.310
We're just going to assume that once they click add, then they're happy with the email and password.

02:33.950 --> 02:39.110
So now that we've cut down that little bit of code, it makes this a little bit easier to understand.

02:39.470 --> 02:45.030
All that we're doing here is we're checking to make sure that the length of the website entry and the

02:45.030 --> 02:48.390
length of the password entry is equal to zero.

02:48.830 --> 02:52.670
And you might still have the length checking for the email entry.

02:52.870 --> 03:00.150
But remember that at the end of previous lessons, we actually inserted a default value so we can actually

03:00.150 --> 03:02.710
skip that check as well if you wanted to.

03:03.910 --> 03:09.200
The reason why we want all code as simple as possible is so that when we're working with the JSON and

03:09.200 --> 03:14.320
we're learning about how to work with it, as long as everything else is simple, then we can focus

03:14.320 --> 03:16.080
on the new things that we're learning.

03:16.680 --> 03:24.080
In this case, instead of opening a data dot txt file, I'm going to change the data format to a dot

03:24.120 --> 03:32.720
JSON and instead of using the append mode, I'm going to use the write mode because I'm going to be

03:32.720 --> 03:35.920
writing to this data dot JSON file.

03:36.440 --> 03:40.880
Now, in order to write to a JSON file, we don't use this line of code.

03:41.280 --> 03:45.440
Instead we use a method called JSON dot dump.

03:46.360 --> 03:53.480
And that of course requires us to import the JSON module, which should be inbuilt to Python so you

03:53.480 --> 03:55.200
don't actually have to install it.

03:55.800 --> 04:01.600
Now, once we've imported our JSON module we can say JSON dot dump.

04:02.080 --> 04:04.800
And this takes a number of inputs.

04:04.800 --> 04:10.580
But the most important are the things that you want to dump and the file that you want to dump it to.

04:11.020 --> 04:14.700
The data that we want to put in here should go in as a dictionary.

04:15.180 --> 04:20.580
Essentially, what we want to create is a new dictionary, which I'll call new data.

04:20.900 --> 04:23.180
And it's going to be a nested dictionary.

04:23.540 --> 04:28.460
So the first level key is going to be the website, because this is what we're going to be searching

04:28.460 --> 04:29.580
through eventually.

04:29.980 --> 04:33.740
And the website is going to itself contain a dictionary.

04:34.340 --> 04:41.220
Now this dictionary contains two keys the email and also the password.

04:42.460 --> 04:45.540
The values for each of these are pretty self-explanatory.

04:45.540 --> 04:52.420
It's the email that we got from this line of code, and the password is the password that we got from

04:52.420 --> 04:53.540
this line of code.

04:55.700 --> 05:01.100
Now that we've created this new dictionary called New Data, well, that is what we're going to use

05:01.100 --> 05:03.700
to dump into our JSON file.

05:03.980 --> 05:06.580
Let's go ahead and dump our new data.

05:06.630 --> 05:08.190
So that's the first input.

05:08.630 --> 05:13.510
Now the next input is going to be the data file that we want to put it into.

05:13.830 --> 05:17.390
So that is going to be the file that we opened up inside this line.

05:18.110 --> 05:22.030
Let's provide our data file as the location to dump this data.

05:22.470 --> 05:31.670
And now if I go ahead and hit run and I create a new entry for my Amazon website password, generate

05:31.670 --> 05:33.510
the password, hit add.

05:34.510 --> 05:40.150
Then you can see that we've got our brand new data JSON file that's just been created.

05:40.590 --> 05:46.230
So just as a quick reminder, when you open a file in write mode, if that file doesn't exist, it will

05:46.270 --> 05:47.510
actually create it.

05:47.790 --> 05:50.670
So we've created this new file data dot JSON.

05:50.670 --> 05:54.990
And we've dumped the data that we entered into that file.

05:54.990 --> 05:59.750
So now if I open this up you can see we've got a JSON format in here.

05:59.910 --> 06:04.510
And we've got our Amazon website and all of its associated data.

06:05.510 --> 06:09.690
Notice how this data is not very easy to read for a human.

06:10.130 --> 06:14.450
So we can actually improve that by adding one other argument.

06:14.610 --> 06:16.530
And it's an argument called indent.

06:16.890 --> 06:23.770
This we can provide the number of spaces to indent all the JSON data so that it becomes much easier

06:23.770 --> 06:24.490
to read.

06:24.650 --> 06:27.450
So I'm going to go ahead and delete everything that's in here.

06:27.850 --> 06:30.290
And I'm going to run the code again.

06:31.130 --> 06:39.450
And this time once I save this data into my data dot JSON, you can see everything is all indented and

06:39.450 --> 06:41.930
it's much easier for the human to read.

06:43.010 --> 06:50.450
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

06:50.450 --> 06:54.610
can load data from the JSON file, or how to read from it.

06:55.610 --> 07:00.570
To read JSON data, we use the JSON module and we call the load method.

07:01.010 --> 07:03.810
So I'm going to go ahead and comment out this line of code.

07:04.130 --> 07:11.460
And now that I've actually got some data in my JSON data file, I'm going to try and read from it.

07:11.500 --> 07:16.500
We call the JSON dot load method and we pass in the file path.

07:16.500 --> 07:18.380
Or you can see here the PHP.

07:18.900 --> 07:21.660
So that's the only required input.

07:22.020 --> 07:28.100
The file that we're going to pass in is our data file which we opened up over here.

07:28.620 --> 07:35.660
And what we're going to do is we're going to change this from write mode to read mode.

07:36.740 --> 07:42.020
Once we've loaded up the data we're going to save it inside a variable called data.

07:42.180 --> 07:43.860
And then I'm just going to print it out.

07:44.340 --> 07:50.180
So when I hit run right now and I simply add any sort of gobbledygook.

07:50.340 --> 07:55.260
But as long as I can hit the add button, it's going to trigger this part of the code.

07:55.660 --> 08:02.540
And now if we take a look inside our console, you can see that data is being printed down here, but

08:02.540 --> 08:06.180
without any of these indentations or any of the formatting.

08:06.460 --> 08:15.600
Instead, what that load method does is it essentially takes this JSON data and converts it into a Python

08:15.600 --> 08:16.480
dictionary.

08:17.040 --> 08:24.920
So this data, if we actually do a type check on it, you can see has a type of dictionary.

08:24.920 --> 08:27.760
This is just a normal Python dictionary.

08:28.400 --> 08:37.400
So essentially we can use Json.dumps and Json.load to serialize and deserialize from JSON data to Python

08:37.400 --> 08:38.240
dictionaries.

08:38.520 --> 08:41.800
And it allows us that free interchange of information.

08:42.440 --> 08:47.760
We change it into a JSON to store it, and then we take it out of storage and turn it into a Python

08:47.760 --> 08:50.640
dictionary to easily work with it in our code.

08:51.720 --> 08:57.960
Now that we've seen how to write, how to read, the last thing I want to show you is how to update

08:57.960 --> 08:58.600
data.

08:58.920 --> 09:05.360
Because if we have a new piece of data come in, we want to add to this JSON, but we don't want to

09:05.400 --> 09:06.440
overwrite it.

09:06.840 --> 09:10.080
But we also don't want to just append to the end of it.

09:10.080 --> 09:14.170
Because you can imagine if we had something that we just appended to the end.

09:14.410 --> 09:16.970
That's not a valid data structure.

09:16.970 --> 09:19.290
And that's why we're getting all of these errors.

09:19.810 --> 09:24.930
Instead of doing it manually, we're going to use the built in method JSON dot update.

09:25.250 --> 09:26.810
And here's how it works.

09:27.210 --> 09:31.330
So we've already got some data inside our data dot JSON file.

09:31.330 --> 09:38.170
And what we want to do is we want to take that data and we want to update what's existing in there with

09:38.170 --> 09:40.490
the new data that's being passed in.

09:41.650 --> 09:45.410
To do that, we first have to load up the data.

09:45.450 --> 09:47.530
So I'm going to uncomment this line.

09:47.810 --> 09:54.570
And once we've gotten hold of the data we're going to say data dot update.

09:56.450 --> 09:59.490
And we update it with the new data.

09:59.850 --> 10:06.090
What's happened so far is we have our data loaded up into a dictionary.

10:06.490 --> 10:12.330
We use the update method to update that dictionary with some new piece of data.

10:13.030 --> 10:19.590
Now, the next thing we want to do is we want to actually write that data back into this file.

10:19.870 --> 10:24.670
So we have to use this line of code using JSON dump.

10:26.910 --> 10:32.870
But instead of dumping the new data we're going to dump that data that we updated right here.

10:34.310 --> 10:38.190
Essentially what we're doing here is a three step approach.

10:38.430 --> 10:40.510
We're reading the old data.

10:41.710 --> 10:46.350
We're updating old data with new data.

10:46.830 --> 10:49.910
And we're finally saving the updated data.

10:50.910 --> 10:52.470
Here we've got two things happening.

10:52.470 --> 10:55.110
We've got reading and we've got writing.

10:55.470 --> 10:59.190
So we can split this code into two sections.

10:59.510 --> 11:02.910
First we open the file in read mode.

11:03.150 --> 11:10.070
We get hold of the data, we update the data, and then we open up the file again.

11:10.630 --> 11:13.560
But in this case we open it in write mode.

11:15.000 --> 11:23.280
This time we're going to dump the data that we've updated over here into that data file using this particular

11:23.280 --> 11:24.360
indent format.

11:25.560 --> 11:31.480
And once we've written to file and we delete all the text from our website and password entries, and

11:31.480 --> 11:38.000
these lines of code now replace our previous functionality where we were saving just plain text to our

11:38.000 --> 11:39.080
data dot text.

11:39.600 --> 11:41.720
Let's go ahead and run it and test it out.

11:42.120 --> 11:46.080
Currently we have a single entry inside our data dot JSON.

11:46.520 --> 11:52.880
So if we go ahead and add a new entry say for eBay and we generate password, click add.

11:53.600 --> 11:58.760
If we go back to our data dot JSON, you can see how it's now been updated.

11:59.200 --> 12:06.520
This entire JSON has now been updated to have two entries Amazon and eBay.

12:06.880 --> 12:11.680
It didn't just appended it actually added it into the dictionary.

12:11.680 --> 12:14.440
And that's all thanks to that update method.

12:14.780 --> 12:21.860
and once it updated that data, then we told it to save the updated data back into the data file and

12:21.860 --> 12:24.020
wipe all of the previous data.

12:24.900 --> 12:32.820
So now that we're able to read, write, and update JSON data, the next step is to think about what

12:32.860 --> 12:35.140
situations might this fail?

12:35.740 --> 12:43.220
And one of the biggest problems we have at the moment is we're trying to open up this file, load up

12:43.220 --> 12:45.180
the data and update it.

12:45.540 --> 12:52.060
But if we actually had no data in here, say the first time we ran our program, or indeed if we didn't

12:52.100 --> 12:59.620
actually have this data dot JSON file, then you can see that the first time I run it and I try to save

12:59.620 --> 13:00.500
a password.

13:02.540 --> 13:04.260
Then it's going to crash.

13:04.500 --> 13:07.300
And we get some exceptions being thrown.

13:07.580 --> 13:09.820
But we know all about exceptions now.

13:09.900 --> 13:15.260
So in the next lesson that's what we're going to tackle for all of that and more.

13:15.460 --> 13:16.660
Head over to the next lesson.
