1 00:00:00,600 --> 00:00:02,120 - [Instructor] In this Self Check exercise, 2 00:00:02,120 --> 00:00:03,800 I'd like you to write out 3 00:00:03,800 --> 00:00:06,100 the grades dict that you see here, 4 00:00:06,100 --> 00:00:08,680 which is a dictionary object in Python. 5 00:00:08,680 --> 00:00:10,140 We'd like you to store that as 6 00:00:10,140 --> 00:00:14,210 Java Script Object Notation in the file grades.JSON. 7 00:00:14,210 --> 00:00:17,820 Then we'd like you to read that file's contents back in 8 00:00:17,820 --> 00:00:22,420 and display the resulting object in pretty printed 9 00:00:22,420 --> 00:00:25,720 JSON format and remember, you can do that with the 10 00:00:25,720 --> 00:00:29,930 dumps function that was part of the JSON module. 11 00:00:29,930 --> 00:00:32,380 So go ahead and pause the video to give that a shot, 12 00:00:32,380 --> 00:00:34,230 and then come back to see the answer. 13 00:00:39,350 --> 00:00:42,880 Okay, let's go ahead and import the JSON module, 14 00:00:42,880 --> 00:00:45,440 and define the dictionary. 15 00:00:45,440 --> 00:00:47,150 And I'll scroll down for the 16 00:00:47,150 --> 00:00:48,960 answer to the other two parts. 17 00:00:48,960 --> 00:00:50,850 So this next snippet is going 18 00:00:50,850 --> 00:00:55,170 to create the file grades.JSON by opening it for writing. 19 00:00:55,170 --> 00:00:58,990 We'll treat that object as the name grades 20 00:00:58,990 --> 00:01:01,300 or assign it to the variable grades 21 00:01:01,300 --> 00:01:03,220 and we will then use 22 00:01:03,220 --> 00:01:05,840 the JSON dump function to take 23 00:01:05,840 --> 00:01:09,030 that grades dict object we defined up above 24 00:01:09,030 --> 00:01:10,640 and write it into the file 25 00:01:10,640 --> 00:01:14,340 that the grades file object refers to. 26 00:01:14,340 --> 00:01:16,640 So let's go ahead and execute that. 27 00:01:16,640 --> 00:01:19,500 At this point, we've created the JSON file 28 00:01:19,500 --> 00:01:22,430 and actually you can see it down here 29 00:01:22,430 --> 00:01:26,720 in the Jupiter Lab listing over on the left side here. 30 00:01:26,720 --> 00:01:27,990 In fact, you know what, let's go ahead 31 00:01:27,990 --> 00:01:31,180 and open that, and notice that it's capable 32 00:01:31,180 --> 00:01:34,640 of giving you this nice, interactive presentation 33 00:01:34,640 --> 00:01:38,530 of what that JSON object looks like in the file. 34 00:01:38,530 --> 00:01:41,372 So that's kind of a cool little feature of Jupiter, 35 00:01:41,372 --> 00:01:43,100 in this case. 36 00:01:43,100 --> 00:01:45,240 But now that we have the file, we can read 37 00:01:45,240 --> 00:01:46,890 the contents back in 38 00:01:46,890 --> 00:01:50,040 and we're going to do that by opening the file for reading. 39 00:01:50,040 --> 00:01:52,660 Again, we'll call the file object grades. 40 00:01:52,660 --> 00:01:55,620 We'll load the contents of that file which will 41 00:01:55,620 --> 00:02:00,620 create an object out of that Java Script Object Notation. 42 00:02:01,230 --> 00:02:04,550 We're then going to take that object and pass it to dumps 43 00:02:04,550 --> 00:02:09,340 and display the formatted Java Script Object Notation with 44 00:02:09,340 --> 00:02:10,870 indents of four. 45 00:02:10,870 --> 00:02:12,710 So if I go ahead and execute that, 46 00:02:12,710 --> 00:02:16,800 you can see the Java Script Object Notation down here. 47 00:02:16,800 --> 00:02:20,220 Again, it uses double quotes rather than single quotes 48 00:02:20,220 --> 00:02:24,240 for strings in the context of Java Script Object Notation. 49 00:02:24,240 --> 00:02:28,070 The outer set of curly braces represents the entire object. 50 00:02:28,070 --> 00:02:31,840 Our entire object had one key-value pair, which was the 51 00:02:31,840 --> 00:02:34,700 grade book and its list of students. 52 00:02:34,700 --> 00:02:36,650 Each of the three students 53 00:02:36,650 --> 00:02:39,490 that we had in the grade book is represented 54 00:02:39,490 --> 00:02:40,990 as a set of curly braces 55 00:02:40,990 --> 00:02:45,170 which means another Java Script Object Notation object, 56 00:02:45,170 --> 00:02:46,690 and within each of those objects 57 00:02:46,690 --> 00:02:50,040 we have key value pairs representing each student's 58 00:02:50,040 --> 00:02:52,403 id, name, and grade.