WEBVTT 1 00:00:00.100 --> 00:00:01.290 Hi and welcome back. 2 00:00:01.290 --> 00:00:04.760 In this video we're talking about JSON files. 3 00:00:04.760 --> 00:00:07.660 JSON is probably one of the most if not the most 4 00:00:07.660 --> 00:00:10.263 popular format for data in the world. 5 00:00:11.110 --> 00:00:14.310 Especially extremely popular for web applications, 6 00:00:14.310 --> 00:00:16.090 and that's because JSON comes from 7 00:00:16.090 --> 00:00:19.860 the most popular web language, JavaScript. 8 00:00:19.860 --> 00:00:24.860 Indeed, JSON stands for JavaScript object notation. 9 00:00:26.010 --> 00:00:28.550 Here's what some JSON could look like. 10 00:00:28.550 --> 00:00:31.240 You've got some sort of curly braces 11 00:00:31.240 --> 00:00:35.310 at the start and at the end, then you've got a key, 12 00:00:35.310 --> 00:00:38.040 around it then our double quotation marks, 13 00:00:38.040 --> 00:00:39.930 that's important, and then you've got a colon, 14 00:00:39.930 --> 00:00:42.430 and you can have a value associated with that key. 15 00:00:43.540 --> 00:00:46.563 Does it remind you of anything in Python? 16 00:00:48.460 --> 00:00:50.480 I hope that it reminds you of a dictionary, 17 00:00:50.480 --> 00:00:52.700 because it looks pretty similar to a dictionary, 18 00:00:52.700 --> 00:00:55.370 in fact it looks the same as a dictionary. 19 00:00:55.370 --> 00:00:57.260 There are just a couple of differences 20 00:00:57.260 --> 00:01:00.000 between JSON, that is this thing here, 21 00:01:00.000 --> 00:01:01.313 and a Python dictionary. 22 00:01:03.350 --> 00:01:05.860 The first and most important difference 23 00:01:05.860 --> 00:01:07.350 is something you have to remember 24 00:01:07.350 --> 00:01:09.730 whenever you deal with JSON, and that is 25 00:01:09.730 --> 00:01:13.300 that JSON is a string, notice how this piece 26 00:01:13.300 --> 00:01:16.120 of JSON here is inside a text file, 27 00:01:16.120 --> 00:01:18.133 and what are the contents of text files? 28 00:01:19.520 --> 00:01:22.090 That's correct, all the contents of text files 29 00:01:22.090 --> 00:01:25.760 are strings, as we saw when we read our CSV file, 30 00:01:25.760 --> 00:01:29.413 and as we saw when we read our plain old data file. 31 00:01:30.940 --> 00:01:33.810 Text files always contain strings only. 32 00:01:33.810 --> 00:01:36.520 When we read this it will be a string, 33 00:01:36.520 --> 00:01:39.153 and we will convert it into a Python dictionary. 34 00:01:40.950 --> 00:01:43.730 The second difference is that JSON must always 35 00:01:43.730 --> 00:01:47.710 use double quotation marks and not single quotation marks. 36 00:01:47.710 --> 00:01:51.910 So if you do this, which is perfectly valid in Python, 37 00:01:51.910 --> 00:01:54.110 it will not be valid JSON, so don't do that. 38 00:01:55.800 --> 00:01:58.860 And some programmes that read and write JSON 39 00:01:58.860 --> 00:02:02.960 require that the outermost element here, the outermost 40 00:02:02.960 --> 00:02:06.373 construct, the outermost structure, be an object. 41 00:02:07.340 --> 00:02:10.720 This thing here that we call a dictionary in Python 42 00:02:10.720 --> 00:02:13.563 is an object in JavaScript, they're very similar. 43 00:02:14.620 --> 00:02:16.110 When talking about JSON, the structure 44 00:02:16.110 --> 00:02:18.640 with these curly braces we call it an object, 45 00:02:18.640 --> 00:02:20.610 because that's what it's called in JavaScript, 46 00:02:20.610 --> 00:02:23.230 I know that in Python they're called dictionaries. 47 00:02:23.230 --> 00:02:26.070 Some programmes however, and that's important, 48 00:02:26.070 --> 00:02:29.300 don't have this requirement, so in some programmes, 49 00:02:29.300 --> 00:02:32.570 you could have a file called friendsJSON.txt, 50 00:02:32.570 --> 00:02:37.570 that is just this, and this would be okay for some programmes. 51 00:02:37.830 --> 00:02:39.070 And notice how the indentation here 52 00:02:39.070 --> 00:02:42.163 is slightly off, and let me fix it real quick. 53 00:02:44.400 --> 00:02:47.190 This would be okay for some programmes but not others. 54 00:02:47.190 --> 00:02:49.910 Some have the requirement that the outermost structure 55 00:02:49.910 --> 00:02:52.640 be an object, that's a dictionary, 56 00:02:52.640 --> 00:02:55.540 some do not have this requirement, and this would be fine. 57 00:02:56.410 --> 00:02:59.080 For our example, we are going to stick 58 00:02:59.080 --> 00:03:02.073 with our object as the outermost structure. 59 00:03:03.980 --> 00:03:06.920 So let's read some JSON using Python. 60 00:03:06.920 --> 00:03:09.260 Python comes with a built in module 61 00:03:09.260 --> 00:03:13.410 that you can import and use called the JSON module. 62 00:03:13.410 --> 00:03:16.000 We're going to talk a lot more about importing 63 00:03:16.000 --> 00:03:17.750 as we move on in this section, 64 00:03:17.750 --> 00:03:21.560 we're going to be importing our own files and modules 65 00:03:21.560 --> 00:03:23.870 as well as system modules, so let's start 66 00:03:23.870 --> 00:03:25.703 with importing the JSON module. 67 00:03:26.930 --> 00:03:29.350 But before we import the JSON module itself, 68 00:03:29.350 --> 00:03:31.683 let's talk a little bit about importing. 69 00:03:33.330 --> 00:03:36.000 The first thing I'm going to do is create a new file 70 00:03:36.000 --> 00:03:40.203 that I'm going to call something like JSON import. 71 00:03:41.410 --> 00:03:44.590 Important, do not call this file JSON, 72 00:03:44.590 --> 00:03:47.730 because the JSON module already exists in Python, 73 00:03:47.730 --> 00:03:50.254 we're going to import it like so. 74 00:03:50.254 --> 00:03:51.760 So if you call your file JSON, 75 00:03:51.760 --> 00:03:54.270 that's going to confuse Python. 76 00:03:54.270 --> 00:03:58.160 Try to not have your files or any of your packages 77 00:03:58.160 --> 00:04:01.460 named after something that already exists in Python, 78 00:04:01.460 --> 00:04:04.080 or indeed try not to have duplicate names for files 79 00:04:04.080 --> 00:04:07.660 on things like that, just a general piece of advice there. 80 00:04:07.660 --> 00:04:09.980 So in Python, as we've done a couple of times 81 00:04:09.980 --> 00:04:12.090 before in the course, sort of glanced 82 00:04:12.090 --> 00:04:14.800 over it, we can import things. 83 00:04:14.800 --> 00:04:17.420 And these things are code that other developers 84 00:04:17.420 --> 00:04:20.270 have written so that we don't have to write it ourselves. 85 00:04:21.400 --> 00:04:23.360 And now we're importing the JSON module, 86 00:04:23.360 --> 00:04:26.170 which contains some functions that allow us 87 00:04:26.170 --> 00:04:28.780 to import the JSON file and convert it 88 00:04:28.780 --> 00:04:31.600 into a dictionary, and also to go the other way around, 89 00:04:31.600 --> 00:04:34.120 turn a dictionary into JSON data. 90 00:04:34.120 --> 00:04:36.090 Remember JSON data is very much 91 00:04:36.090 --> 00:04:38.423 like a dictionary, but it is a string. 92 00:04:40.380 --> 00:04:44.160 And remember that someone did write this JSON module, 93 00:04:44.160 --> 00:04:46.080 in the same way that we are writing this file, 94 00:04:46.080 --> 00:04:48.080 and somebody wrote all the other built in functions 95 00:04:48.080 --> 00:04:53.080 in Python, like len, or sum, or even like 96 00:04:55.070 --> 00:04:58.790 things like print, all of these have been written 97 00:04:58.790 --> 00:05:02.430 by someone, and they are made available to us 98 00:05:02.430 --> 00:05:06.480 to use globally in all our Python files. 99 00:05:06.480 --> 00:05:08.570 The only difference is that this JSON module 100 00:05:08.570 --> 00:05:12.000 is not included by default on all your files, 101 00:05:12.000 --> 00:05:14.100 because whoever created Python 102 00:05:14.100 --> 00:05:17.710 thought that it may not be necessary in all cases. 103 00:05:17.710 --> 00:05:21.210 So the JSON module is a module that comes with Python, 104 00:05:21.210 --> 00:05:22.863 but it is not a global module. 105 00:05:23.970 --> 00:05:26.200 We must make it explicit that we want to use it 106 00:05:26.200 --> 00:05:28.763 by importing it at the top of the file like so. 107 00:05:30.360 --> 00:05:33.930 So we've created a file that contains our friends as JSON, 108 00:05:33.930 --> 00:05:35.840 and notice how this is a friends key, 109 00:05:35.840 --> 00:05:38.280 and that is associated with a list, 110 00:05:38.280 --> 00:05:41.800 and that contains three objects inside of it. 111 00:05:41.800 --> 00:05:43.920 I'm gonna try really hard to call these objects 112 00:05:43.920 --> 00:05:46.060 when I'm talking about JSON, and dictionaries 113 00:05:46.060 --> 00:05:47.380 when I'm talking about Python, 114 00:05:47.380 --> 00:05:50.080 but please bear with me if I do make any mistakes. 115 00:05:50.080 --> 00:05:53.140 These are objects in JSON, dictionaries in Python, 116 00:05:53.140 --> 00:05:55.853 but only when we import them using the JSON module. 117 00:05:57.750 --> 00:06:01.050 Then, we've got the JSON file, sorry, 118 00:06:01.050 --> 00:06:04.930 the JSON module imported, and now we have the ability 119 00:06:04.930 --> 00:06:08.340 to do things like JSON dot, and access, 120 00:06:08.340 --> 00:06:10.130 as long as you're using PyCharm or something else 121 00:06:10.130 --> 00:06:12.440 that will recommend functions, 122 00:06:12.440 --> 00:06:15.300 we can access the functions in that module. 123 00:06:15.300 --> 00:06:17.950 Notice how this JSON module is not an object, 124 00:06:17.950 --> 00:06:20.020 it's not a class, because we didn't have to 125 00:06:20.020 --> 00:06:25.020 create a new JSON object in order to access things. 126 00:06:25.410 --> 00:06:28.290 This module here allows us to use some functions, 127 00:06:28.290 --> 00:06:31.160 but they are not inside a class. 128 00:06:31.160 --> 00:06:33.260 And indeed, in PyCharm you can go 129 00:06:33.260 --> 00:06:38.260 into the JSON module, by clicking Command + click, 130 00:06:38.860 --> 00:06:41.153 or Control + click if you're on Windows, 131 00:06:42.170 --> 00:06:46.650 and you can see the source code of the module here. 132 00:06:46.650 --> 00:06:51.640 You can see that a lot of it is these comments 133 00:06:51.640 --> 00:06:53.730 that tell you how to use the functions, 134 00:06:53.730 --> 00:06:56.130 but primarily there are some functions definitions, 135 00:06:56.130 --> 00:06:58.850 like dump, and you can see the source code 136 00:06:58.850 --> 00:07:02.080 and what it does, you've dump S, and you can see 137 00:07:02.080 --> 00:07:04.310 the arguments, and the source code 138 00:07:04.310 --> 00:07:06.230 and all the comments and everything there. 139 00:07:06.230 --> 00:07:08.260 So as you can see this is just code, 140 00:07:08.260 --> 00:07:10.070 like the one we write, except we didn't 141 00:07:10.070 --> 00:07:12.127 write it, but we can still import it. 142 00:07:12.127 --> 00:07:13.970 And notice how as we go through the file 143 00:07:13.970 --> 00:07:15.800 these functions are at the lowest level 144 00:07:15.800 --> 00:07:17.910 of indentation, they are at the very left, 145 00:07:17.910 --> 00:07:20.570 which means they are not inside a class. 146 00:07:20.570 --> 00:07:23.140 So as we import the module, we'll have 147 00:07:23.140 --> 00:07:25.473 access to those functions directly. 148 00:07:26.600 --> 00:07:29.040 Okay, feel free to read through that module if you want, 149 00:07:29.040 --> 00:07:31.160 if you wanna have a wee look at what is in there, 150 00:07:31.160 --> 00:07:33.310 but we are going to be using only 151 00:07:33.310 --> 00:07:35.920 a couple of functions in this video. 152 00:07:35.920 --> 00:07:38.583 The first thing you need is a file pointer. 153 00:07:39.840 --> 00:07:42.580 For that, we're going to use the open function of course, 154 00:07:42.580 --> 00:07:46.560 and we have to use an equals sign, and we're going to open 155 00:07:46.560 --> 00:07:49.410 the friends_JSON.txt for reading, 156 00:07:49.410 --> 00:07:52.700 so you know how to do that, friends_JSON.txt, 157 00:07:52.700 --> 00:07:57.700 in reading mode, then we're going to load the contents. 158 00:07:57.999 --> 00:08:01.220 The way to do that is to file contents for example, 159 00:08:01.220 --> 00:08:05.090 it's JSON.load, of the file pointer. 160 00:08:05.090 --> 00:08:08.600 So the load function inside the JSON module takes in 161 00:08:08.600 --> 00:08:12.920 a file pointer that it's going to load the JSON from. 162 00:08:12.920 --> 00:08:14.850 As part of reading the file, by the way, 163 00:08:14.850 --> 00:08:17.370 this reads the entire file, it also turns it 164 00:08:17.370 --> 00:08:20.413 into a dictionary, so now file contents is a dictionary. 165 00:08:21.880 --> 00:08:24.533 Reads file and turns it to dictionary. 166 00:08:26.530 --> 00:08:29.160 Then of course we can close the file, 167 00:08:29.160 --> 00:08:31.690 and now we can use the file contents 168 00:08:31.690 --> 00:08:33.860 for whatever we want, for example, 169 00:08:33.860 --> 00:08:38.860 we can print file contents friends, zero. 170 00:08:38.890 --> 00:08:42.173 And that should print this dictionary here. 171 00:08:46.240 --> 00:08:49.160 Okay, remember if you're using PyCharm, 172 00:08:49.160 --> 00:08:52.420 you may be tempted to press the top right play button, 173 00:08:52.420 --> 00:08:55.000 but it's likely that that's gonna play the wrong file, 174 00:08:55.000 --> 00:08:57.890 CSV_read in this case, so if that's the case, 175 00:08:57.890 --> 00:09:00.713 just right click the JSON import file, and run it. 176 00:09:02.370 --> 00:09:05.300 And there you have it, we print out the dictionary 177 00:09:05.300 --> 00:09:07.970 with name Jose and degree applied computing. 178 00:09:07.970 --> 00:09:09.770 That's what I studied at university. 179 00:09:11.855 --> 00:09:15.300 And this is how you load a JSON file into a dictionary 180 00:09:15.300 --> 00:09:17.460 that you can then use as normal, 181 00:09:17.460 --> 00:09:19.860 so here we're accessing the friends key, and then we're 182 00:09:19.860 --> 00:09:22.463 accessing the zeroth element of the friends key. 183 00:09:25.730 --> 00:09:27.070 If you wanted to do the opposite, 184 00:09:27.070 --> 00:09:28.680 and you want to write a Python dictionary 185 00:09:28.680 --> 00:09:32.410 as a JSON file, that's also something you can do. 186 00:09:32.410 --> 00:09:34.720 For example, we are going to write into a file 187 00:09:34.720 --> 00:09:38.120 called cars_JSON, so we're gonna do 188 00:09:38.120 --> 00:09:42.453 cars equal this list here of dictionaries. 189 00:09:48.670 --> 00:09:50.900 And by the way, something I tend to do, 190 00:09:50.900 --> 00:09:53.120 just because I programme a lot on JavaScript 191 00:09:53.120 --> 00:09:56.060 is have this space after the dictionary. 192 00:09:56.060 --> 00:09:58.840 And Python doesn't really like that, 193 00:09:58.840 --> 00:10:00.990 so if I do that I apologise, I just find it 194 00:10:00.990 --> 00:10:04.030 a bit more readable, but that's just because 195 00:10:04.030 --> 00:10:06.080 I do a lot of JavaScript and that's 196 00:10:06.080 --> 00:10:08.603 a convention in JavaScript as well. 197 00:10:11.250 --> 00:10:14.920 So now we have two dictionaries inside our list, 198 00:10:14.920 --> 00:10:16.410 and of course as I said earlier, 199 00:10:16.410 --> 00:10:19.000 we can totally save this as JSON, 200 00:10:19.000 --> 00:10:21.330 it doesn't need to have a dictionary enclosing it, 201 00:10:21.330 --> 00:10:25.720 although some programmes may request that you have that. 202 00:10:25.720 --> 00:10:28.360 So again, if you want to save this to a dictionary, 203 00:10:28.360 --> 00:10:30.733 you know exactly what to do, open the file, 204 00:10:31.860 --> 00:10:34.663 save the contents into the file, and then close it. 205 00:10:35.540 --> 00:10:37.520 How you're gonna save the contents? 206 00:10:37.520 --> 00:10:42.150 Instead of JSON.load, you're gonna use JSON.dump. 207 00:10:42.150 --> 00:10:44.340 So here's what you have to do. 208 00:10:44.340 --> 00:10:48.213 Go and investigate how to use JSON.dump. 209 00:10:49.430 --> 00:10:52.630 Or optionally, just wait until after I ask you, 210 00:10:52.630 --> 00:10:54.190 pause the video and you try this yourself, 211 00:10:54.190 --> 00:10:56.143 and I will implement this for you. 212 00:10:57.030 --> 00:10:59.540 If you are so inclined, pause the video now 213 00:10:59.540 --> 00:11:02.640 and give this a go, investigate how to use JSON.dump, 214 00:11:02.640 --> 00:11:06.090 and then save this list into a new file 215 00:11:06.090 --> 00:11:09.470 that you can call cars_JSON.txt. 216 00:11:09.470 --> 00:11:10.970 I'll see you in a few seconds. 217 00:11:14.730 --> 00:11:17.470 I hope you got that, if you did go and investigate 218 00:11:17.470 --> 00:11:20.040 JSON.dump, congratulations, well done, 219 00:11:20.040 --> 00:11:23.030 by investigating you're gonna learn faster. 220 00:11:23.030 --> 00:11:25.410 And there are multiple ways you could have investigated, 221 00:11:25.410 --> 00:11:28.200 you could have Googled JSON.dump Python, 222 00:11:28.200 --> 00:11:29.970 and one of those first few links 223 00:11:29.970 --> 00:11:33.150 would be the official Python documentation. 224 00:11:33.150 --> 00:11:34.570 Another option, you could have gone 225 00:11:34.570 --> 00:11:36.790 into the JSON module with Command + click 226 00:11:36.790 --> 00:11:39.030 if you're on Mac, or Control + click 227 00:11:39.030 --> 00:11:42.690 if you're on Windows, and then you could have found 228 00:11:42.690 --> 00:11:47.690 this documentation here for the dump function, 229 00:11:48.120 --> 00:11:50.120 then it tells you what it does. 230 00:11:50.120 --> 00:11:53.550 It serialises the obj parameter 231 00:11:53.550 --> 00:11:56.660 as a JSON formatted string to fp, 232 00:11:56.660 --> 00:12:00.140 A .write supporting file-like object. 233 00:12:00.140 --> 00:12:02.010 Now I appreciate this is some lingo 234 00:12:02.010 --> 00:12:05.933 that we've got here, serialising means turning to a string. 235 00:12:07.020 --> 00:12:10.270 Fp is normally a file pointer, and it tells you 236 00:12:10.270 --> 00:12:12.070 here that it has to be a file-like 237 00:12:12.070 --> 00:12:14.980 object that allows you to do .write. 238 00:12:14.980 --> 00:12:17.220 That's what we've been doing with our text files 239 00:12:17.220 --> 00:12:18.910 when we've been writing to them. 240 00:12:18.910 --> 00:12:23.360 So, this dump takes in a object, and that's gonna 241 00:12:23.360 --> 00:12:25.973 get turned into a string and saved into fp. 242 00:12:26.940 --> 00:12:29.160 Therefore, if we go back to our JSON import, 243 00:12:29.160 --> 00:12:31.193 what we have to do, is open the file, 244 00:12:34.100 --> 00:12:37.823 in write mode this time, and then say JSON.dump, 245 00:12:38.800 --> 00:12:43.800 cars, and file, and then of course file.close. 246 00:12:44.320 --> 00:12:47.530 Let's run that, notice how it runs, 247 00:12:47.530 --> 00:12:52.003 cars_JSON gets created, and it contains our dumped JSON. 248 00:12:53.040 --> 00:12:57.480 Notice though it's not nicely formatted, and that's fine. 249 00:12:57.480 --> 00:12:59.820 JSON doesn't need any new lines or spaces 250 00:12:59.820 --> 00:13:01.040 or anything like that, all of these 251 00:13:01.040 --> 00:13:04.800 are completely optional, just notice how it's used 252 00:13:04.800 --> 00:13:08.370 double quotation marks, as that is important even though 253 00:13:08.370 --> 00:13:10.890 we used single quotation marks in our dictionaries. 254 00:13:10.890 --> 00:13:13.470 Python does not care for our strings, 255 00:13:13.470 --> 00:13:15.650 whether we use double or single quotation marks, 256 00:13:15.650 --> 00:13:17.763 but JSON does, so it uses that. 257 00:13:20.820 --> 00:13:24.760 If you had a string variable that you wanted 258 00:13:24.760 --> 00:13:27.380 to turn into a dictionary, you can do that, 259 00:13:27.380 --> 00:13:32.303 for example let's say my JSON string is something like this. 260 00:13:39.670 --> 00:13:40.670 This is completely wrong by the way, 261 00:13:40.670 --> 00:13:42.640 I know very little about cars, 262 00:13:42.640 --> 00:13:45.180 let's say you've got this JSON string 263 00:13:45.180 --> 00:13:48.020 of this Alfa Romeo car that was released in 1950, 264 00:13:48.020 --> 00:13:51.010 probably pretty wrong, I'm guessing it's more like 265 00:13:51.010 --> 00:13:53.260 much later, but none the less, let's say 266 00:13:53.260 --> 00:13:54.680 you have this JSON string here, 267 00:13:54.680 --> 00:13:56.680 and you wanna turn it into a dictionary. 268 00:13:57.810 --> 00:13:59.560 You can do that, but this time, 269 00:13:59.560 --> 00:14:03.270 instead of using load, that turns a JSON file 270 00:14:03.270 --> 00:14:08.270 into a dictionary, we're gonna use load S, for load string. 271 00:14:09.030 --> 00:14:14.030 So, incorrect car is going to be JSON.loads, 272 00:14:15.610 --> 00:14:18.480 and notice how the first argument is a string, 273 00:14:18.480 --> 00:14:20.370 or rather S, which stands for string, 274 00:14:20.370 --> 00:14:22.480 and that's gonna be my JSON string, 275 00:14:22.480 --> 00:14:27.040 and then you can print incorrect car name, Bane. 276 00:14:27.040 --> 00:14:30.600 We're going Batman here, name, there we go. 277 00:14:30.600 --> 00:14:34.040 Then you can print that, oh sorry, 278 00:14:34.040 --> 00:14:36.520 it's in a list, so of course you have to access 279 00:14:36.520 --> 00:14:38.870 element zero first, my bad, if you noticed that, 280 00:14:38.870 --> 00:14:42.693 well done, and there you have Alfa Romeo printed out. 281 00:14:44.480 --> 00:14:47.470 So load S allows you to turn a JSON string 282 00:14:47.470 --> 00:14:50.860 into a dictionary, and of course dump S 283 00:14:50.860 --> 00:14:54.110 allows you to turn a dictionary into a string, 284 00:14:54.110 --> 00:14:55.160 pretty straightforward to use, 285 00:14:55.160 --> 00:14:56.460 I won't give you an example of that, 286 00:14:56.460 --> 00:14:59.163 'cause if you have a dictionary, 287 00:15:00.555 --> 00:15:03.550 or a JSON thing, like this one, like this list, 288 00:15:03.550 --> 00:15:06.890 or a dictionary, you can use dump S to turn 289 00:15:06.890 --> 00:15:10.773 whatever construct you've got into JSON, a JSON string. 290 00:15:12.530 --> 00:15:16.850 So JSON allows you to use lists and dictionaries, 291 00:15:16.850 --> 00:15:19.253 it does not allow you to use topples. 292 00:15:20.330 --> 00:15:22.590 So that's important, just make sure to use lists 293 00:15:22.590 --> 00:15:24.410 and dictionaries, don't use topples, 294 00:15:24.410 --> 00:15:25.970 you can use pretty much anything else, 295 00:15:25.970 --> 00:15:28.940 like strings, and numbers, floats, 296 00:15:28.940 --> 00:15:32.680 and the JSON module will automatically call 297 00:15:32.680 --> 00:15:36.010 the repr function, the dunder repr function of your object, 298 00:15:36.010 --> 00:15:38.370 if you choose to include any object in here, 299 00:15:38.370 --> 00:15:42.680 generally though try to avoid objects in a JSON, 300 00:15:42.680 --> 00:15:45.290 just because when you load them up, 301 00:15:45.290 --> 00:15:46.620 it's just gonna be a string and it's gonna be 302 00:15:46.620 --> 00:15:49.120 really difficult to understand what's going on. 303 00:15:49.120 --> 00:15:52.830 So if you want to turn your objects into JSON, 304 00:15:52.830 --> 00:15:56.350 which we will do shortly, make sure that you 305 00:15:56.350 --> 00:15:59.030 describe your objects using a dictionary. 306 00:15:59.030 --> 00:16:00.640 For example you can make a dictionary 307 00:16:00.640 --> 00:16:04.650 out of the object's variable properties and their values. 308 00:16:04.650 --> 00:16:06.410 That way you can then export your objects 309 00:16:06.410 --> 00:16:09.340 as JSON string if you want to. 310 00:16:09.340 --> 00:16:11.270 If you don't know what I'm talking about, that's fine, 311 00:16:11.270 --> 00:16:13.260 we're going to be doing just that, 312 00:16:13.260 --> 00:16:16.330 exporting our objects as JSON later on in the course, 313 00:16:16.330 --> 00:16:18.150 and even some other courses I've got 314 00:16:18.150 --> 00:16:20.140 we use things like that as well. 315 00:16:20.140 --> 00:16:22.330 So it's a pretty popular approach 316 00:16:22.330 --> 00:16:24.910 to turn objects and to turn your variables 317 00:16:24.910 --> 00:16:27.730 into JSON so that you can save them into files, 318 00:16:27.730 --> 00:16:30.030 read them nicely, and also so you can send them 319 00:16:30.030 --> 00:16:31.120 over the internet, 'cause that's 320 00:16:31.120 --> 00:16:32.853 a popular thing to do with JSON. 321 00:16:34.040 --> 00:16:35.690 Alright, so that's enough of that, 322 00:16:35.690 --> 00:16:37.890 we've looked at CSVs and JSON, 323 00:16:37.890 --> 00:16:40.780 they're two very popular ways of storing data, 324 00:16:40.780 --> 00:16:42.770 JSON much more popular for sending data 325 00:16:42.770 --> 00:16:44.710 over the internet, another we're able 326 00:16:44.710 --> 00:16:47.070 to read and write data in this variety of formats, 327 00:16:47.070 --> 00:16:49.000 we are good to start creating apps 328 00:16:49.000 --> 00:16:50.940 that persist data into a disc, 329 00:16:50.940 --> 00:16:53.060 save data into our disc, and allows it 330 00:16:53.060 --> 00:16:56.403 to store it between it's run times. 331 00:16:57.460 --> 00:16:59.590 In the next section we're going to be creating 332 00:16:59.590 --> 00:17:04.240 a nice project, but now I wanted to move on to 333 00:17:05.180 --> 00:17:07.650 a couple other things, like importing 334 00:17:07.650 --> 00:17:10.200 our own files and our own modules, 335 00:17:10.200 --> 00:17:12.280 just so you can start dividing your programme 336 00:17:12.280 --> 00:17:14.270 into constituent parts, as opposed 337 00:17:14.270 --> 00:17:16.736 to having everything in a single file. 338 00:17:16.736 --> 00:17:18.200 This is pretty important whenever you're creating 339 00:17:18.200 --> 00:17:20.610 a larger app, you're going to be dividing it 340 00:17:20.610 --> 00:17:23.310 into multiple files, so knowing how to interact 341 00:17:23.310 --> 00:17:26.360 between those two and how the imports work 342 00:17:26.360 --> 00:17:29.500 in a lower level is going to be really important. 343 00:17:29.500 --> 00:17:31.840 With that said, let's move on to the next video. 344 00:17:31.840 --> 00:17:32.790 I'll see you there.