1 00:00:01,630 --> 00:00:03,480 - [Instructor] Next we're going to take a look at 2 00:00:03,480 --> 00:00:08,480 serializing a Python object into JavaScript Object Notation, 3 00:00:09,950 --> 00:00:11,270 and for this demonstration, 4 00:00:11,270 --> 00:00:15,480 we'll be writing that object into a text file. 5 00:00:15,480 --> 00:00:19,620 So first of all, we've created a Python dictionary 6 00:00:19,620 --> 00:00:21,960 in this case, that we'll be writing out 7 00:00:21,960 --> 00:00:24,620 as JavaScript Object Notation. 8 00:00:24,620 --> 00:00:29,300 The accounts dictionary consists of a set of curly braces. 9 00:00:29,300 --> 00:00:32,450 In that set of curly braces, we have one key, 10 00:00:32,450 --> 00:00:34,300 which is the accounts 11 00:00:35,210 --> 00:00:40,190 key, and its value is a list of other dictionaries, 12 00:00:40,190 --> 00:00:44,580 and those two dictionaries represent records of information 13 00:00:44,580 --> 00:00:46,530 in our accounts dictionary. 14 00:00:46,530 --> 00:00:50,540 So in the accounts dictionary, the accounts key has a list 15 00:00:50,540 --> 00:00:54,470 as its object, its value, and within that list 16 00:00:54,470 --> 00:00:58,240 we have a dictionary that represents one account with the 17 00:00:58,240 --> 00:01:02,850 account number 100, the name Jones and the balance 24.98, 18 00:01:02,850 --> 00:01:06,810 and a second account, which has the value 200 for the 19 00:01:06,810 --> 00:01:11,810 account number, the name Doe and the balance 345.67. 20 00:01:11,810 --> 00:01:16,210 So let's now take a look at working with the JSON module, 21 00:01:16,210 --> 00:01:18,600 so we're going to import that module, 22 00:01:18,600 --> 00:01:23,440 and let's use a with statement to open up a file for writing 23 00:01:23,440 --> 00:01:26,480 and output the dictionary 24 00:01:26,480 --> 00:01:29,250 in JavaScript Object Notation format. 25 00:01:29,250 --> 00:01:33,590 So as you can see here we're opening the file accounts.json. 26 00:01:33,590 --> 00:01:36,990 We're using .json as the file name extension here, 27 00:01:36,990 --> 00:01:41,530 but there is no specific required format or 28 00:01:41,530 --> 00:01:45,210 extension for a file containing JSON objects. 29 00:01:45,210 --> 00:01:47,240 We're opening for writing, so that's going to 30 00:01:47,240 --> 00:01:49,510 create the file if it doesn't exist, 31 00:01:49,510 --> 00:01:51,120 and otherwise it's going to wipe out 32 00:01:51,120 --> 00:01:53,330 the contents of an existing file, 33 00:01:53,330 --> 00:01:57,120 and we're going to reference that file object as accounts. 34 00:01:57,120 --> 00:02:00,530 Now in the JSON module, there is a method, 35 00:02:00,530 --> 00:02:04,910 or function, rather, called dump, which takes two arguments. 36 00:02:04,910 --> 00:02:08,750 The first is the Python object that we want 37 00:02:08,750 --> 00:02:12,200 to turn into JavaScript Object Notation, 38 00:02:12,200 --> 00:02:15,720 and the second is the file object into which 39 00:02:15,720 --> 00:02:18,600 we are going to write that information. 40 00:02:18,600 --> 00:02:20,370 So we'll go ahead an execute that, 41 00:02:20,370 --> 00:02:22,360 and remember that's automatically going to 42 00:02:22,360 --> 00:02:24,620 close the file once we're done, 43 00:02:24,620 --> 00:02:28,750 and now let's just go ahead and display the contents 44 00:02:28,750 --> 00:02:32,620 of that accounts.json file so that we can see 45 00:02:32,620 --> 00:02:36,800 what it looks like out on disc, if you will. 46 00:02:36,800 --> 00:02:40,780 By the way again, cat, for those of you who are using 47 00:02:40,780 --> 00:02:43,570 Linux and Mac OS, and you would use more 48 00:02:43,570 --> 00:02:45,900 for those of you who are on Windows. 49 00:02:45,900 --> 00:02:47,750 Now as you can see here, 50 00:02:47,750 --> 00:02:51,850 we have just a long string of information. 51 00:02:51,850 --> 00:02:54,470 JSON is just a big string of data, 52 00:02:54,470 --> 00:02:59,470 and in this string of information we have the key accounts, 53 00:02:59,700 --> 00:03:02,150 which was the key in our dictionary. 54 00:03:02,150 --> 00:03:07,090 We have now a JavaScript Object Notation list, 55 00:03:07,090 --> 00:03:10,600 and within that JavaScript Object Notation list, 56 00:03:10,600 --> 00:03:14,020 we have here a JSON object. 57 00:03:14,020 --> 00:03:17,120 Every JSON object is delimited by curly braces. 58 00:03:17,120 --> 00:03:22,120 This JSON object has three name value pairs as part of it. 59 00:03:22,220 --> 00:03:25,070 Then we have another JSON object in that list, 60 00:03:25,070 --> 00:03:28,430 which also has three name value pairs as part of it. 61 00:03:28,430 --> 00:03:31,760 So it looks just like the dictionary up above. 62 00:03:31,760 --> 00:03:35,080 The main difference is that we have double quote characters 63 00:03:35,080 --> 00:03:37,150 instead of single quote characters, 64 00:03:37,150 --> 00:03:39,790 because in JavaScript Object Notation, 65 00:03:39,790 --> 00:03:43,383 you must use double quote characters for strings. 66 00:03:44,450 --> 00:03:46,600 In the next video, we'll take a look at 67 00:03:46,600 --> 00:03:50,570 reading this JSON object back into our 68 00:03:52,177 --> 00:03:53,453 iPython session.