1 00:00:07,910 --> 00:00:11,160 Welcome back. Nested lists and 2 00:00:11,160 --> 00:00:14,685 dictionaries are very commonly shared between computer systems. 3 00:00:14,685 --> 00:00:18,410 A standard format has been defined that makes it easy to share that data. 4 00:00:18,410 --> 00:00:24,869 It's called JSON, J-S-O-N which stands for JavaScript Object Notation. 5 00:00:24,869 --> 00:00:29,175 It originated with a different programming language JavaScript. 6 00:00:29,175 --> 00:00:34,935 But, Python makes it pretty easy to both read in and write out data in JSON format. 7 00:00:34,935 --> 00:00:38,330 In fact, JSON format looks almost 8 00:00:38,330 --> 00:00:41,695 exactly like the printout of a Python list or a dictionary. 9 00:00:41,695 --> 00:00:44,560 In most cases, you wouldn't really notice the difference, 10 00:00:44,560 --> 00:00:46,670 but there are a couple of small differences; 11 00:00:46,670 --> 00:00:50,060 like the Python value none being represented as the word null, 12 00:00:50,060 --> 00:00:52,550 and true and false not being capitalized. 13 00:00:52,550 --> 00:00:58,430 We'll use the JSON module to read in and write out data in JSON format. 14 00:00:58,430 --> 00:01:03,155 Little warning, the JSON module that we have in Runestone 15 00:01:03,155 --> 00:01:08,080 only does a subset of what the full JSON module and a full Python environment would do. 16 00:01:08,080 --> 00:01:10,730 But we have the two most useful functions. 17 00:01:10,730 --> 00:01:12,470 Let's take a look at them. 18 00:01:12,470 --> 00:01:16,660 The first one is called Load S. It takes a string as input, 19 00:01:16,660 --> 00:01:20,030 and it returns a dictionary or a list. 20 00:01:20,030 --> 00:01:24,065 So, here we have little code snippet. 21 00:01:24,065 --> 00:01:26,735 We're importing the JSON module, 22 00:01:26,735 --> 00:01:33,890 and then we've got a variable called A string whose value is naturally a string, 23 00:01:33,890 --> 00:01:37,430 and that string has some newline characters in it, 24 00:01:37,430 --> 00:01:39,620 and then it's got the curly brace which 25 00:01:39,620 --> 00:01:43,055 would suggest to you that it's going to be a Python dictionary. 26 00:01:43,055 --> 00:01:44,950 It's really just a string, 27 00:01:44,950 --> 00:01:50,885 but once we take that string and on line three, 28 00:01:50,885 --> 00:01:56,195 we pass it as an input to the Load S function, 29 00:01:56,195 --> 00:01:58,735 which is part of the JSON module. 30 00:01:58,735 --> 00:02:00,695 The Load S function, 31 00:02:00,695 --> 00:02:02,000 in the JSON module, 32 00:02:02,000 --> 00:02:05,540 takes a string as an input and as an output it produces 33 00:02:05,540 --> 00:02:07,160 either a Python list or 34 00:02:07,160 --> 00:02:10,840 a Python dictionary depending on what the contents of the string were. 35 00:02:10,840 --> 00:02:17,255 So, in this case the string has its first non blank space its non white space character, 36 00:02:17,255 --> 00:02:19,910 the first one is a curly brace. 37 00:02:19,910 --> 00:02:22,835 So, what's going to be output is a dictionary. 38 00:02:22,835 --> 00:02:24,460 So on line four, 39 00:02:24,460 --> 00:02:27,195 we print out the type of D, 40 00:02:27,195 --> 00:02:29,225 and it is a dictionary. 41 00:02:29,225 --> 00:02:30,950 Since it's a dictionary, 42 00:02:30,950 --> 00:02:36,535 we can ask for its keys and it has two keys; 43 00:02:36,535 --> 00:02:38,430 result count and results. 44 00:02:38,430 --> 00:02:43,505 We can ask for the value associated with result count and it's 25. 45 00:02:43,505 --> 00:02:47,820 That's the 25 that you see there in the string. 46 00:02:48,190 --> 00:02:51,250 One important thing to keep track of here, 47 00:02:51,250 --> 00:02:52,795 is when you have a string, 48 00:02:52,795 --> 00:02:54,260 and when you have a dictionary. 49 00:02:54,260 --> 00:02:59,060 So, we start with something that's a character string, 50 00:02:59,060 --> 00:03:02,810 and by passing it in to the Load S function, 51 00:03:02,810 --> 00:03:06,430 we get a dictionary as an output. 52 00:03:06,430 --> 00:03:08,970 If I lost track of that let's say, 53 00:03:08,970 --> 00:03:16,230 and I tried to print a_string square bracket result count. 54 00:03:16,230 --> 00:03:19,725 Because a_string kind of looks like a dictionary, 55 00:03:19,725 --> 00:03:22,605 and I try to run this, I'm going to get an error. 56 00:03:22,605 --> 00:03:29,480 It says its string indices must be integers not strings on line seven. 57 00:03:29,480 --> 00:03:31,805 Let's say on line seven, 58 00:03:31,805 --> 00:03:35,510 we're trying to treat a_string as if it's a dictionary. 59 00:03:35,510 --> 00:03:37,565 It isn't a dictionary, it's a string. 60 00:03:37,565 --> 00:03:41,810 We're allowed to ask for a_string square bracket four to get the fourth character, 61 00:03:41,810 --> 00:03:45,410 but we can't ask for a_string square bracket result count. 62 00:03:45,410 --> 00:03:49,635 We can ask for D square bracket result count, 63 00:03:49,635 --> 00:03:52,590 because D is a dictionary. 64 00:03:52,590 --> 00:03:57,440 The second useful function from the JSON module is called Dump S. 65 00:03:57,440 --> 00:04:01,814 It does the opposite of Load S. It takes a Python list or dictionary, 66 00:04:01,814 --> 00:04:03,085 even a nested one, 67 00:04:03,085 --> 00:04:07,010 and converts it into a string that's in the JSON format. 68 00:04:07,010 --> 00:04:09,440 Once we have a string in the JSON format, 69 00:04:09,440 --> 00:04:13,885 we can write it to a file or do anything else that we would do with a string. 70 00:04:13,885 --> 00:04:19,955 The Dump S function always takes a list or a dictionary as an input. 71 00:04:19,955 --> 00:04:23,315 But it also can take a couple of other parameters. 72 00:04:23,315 --> 00:04:28,225 If you pass sort keys equals true, then, 73 00:04:28,225 --> 00:04:34,445 whenever you have a dictionary it's going to output those in alphabetic order. 74 00:04:34,445 --> 00:04:38,225 If you pass the indent parameter, 75 00:04:38,225 --> 00:04:40,800 it will pretty print the string. 76 00:04:40,800 --> 00:04:43,090 It'll do some indentation in line breaks. 77 00:04:43,090 --> 00:04:47,720 You can specify how many characters of indentation you want. 78 00:04:47,720 --> 00:04:51,000 So let's see what happens when we run this. 79 00:04:53,270 --> 00:04:56,340 Oops! I got an error. 80 00:04:56,340 --> 00:04:58,515 It's telling me that on line two, 81 00:04:58,515 --> 00:05:01,110 JSON is not defined. 82 00:05:01,110 --> 00:05:03,570 The reason is that I forgot, 83 00:05:03,570 --> 00:05:06,565 and I need to import the JSON module. 84 00:05:06,565 --> 00:05:09,960 So, the Dump S function is in the JSON module, 85 00:05:09,960 --> 00:05:17,885 and this code snippet didn't have the JSON module imported. 86 00:05:17,885 --> 00:05:20,755 So let me run it again. 87 00:05:20,755 --> 00:05:26,610 Now you can see, when I pass D to the pretty function on line nine, 88 00:05:26,610 --> 00:05:35,105 that it is creating a string by calling the json.dumps function and that string, 89 00:05:35,105 --> 00:05:37,600 because I said indent equals two, 90 00:05:37,600 --> 00:05:43,030 each time I descend one level into this next to the data structure, 91 00:05:43,030 --> 00:05:46,400 it's adding two more levels, two more spaces. 92 00:05:46,400 --> 00:05:51,820 That makes it just a lot easier for a person to read when I print out that string. 93 00:05:51,820 --> 00:05:56,320 Now, you may have noticed little funny pronunciation that I've done here. 94 00:05:56,320 --> 00:05:59,260 Instead of calling this Dumps and Loads, 95 00:05:59,260 --> 00:06:03,265 I've called it Dump S and Load S. That's very deliberate. 96 00:06:03,265 --> 00:06:07,730 It helps me to remind me that that S is for string. 97 00:06:07,730 --> 00:06:10,235 The S is not for plural. 98 00:06:10,235 --> 00:06:13,670 You still have to remember which of Dump S and 99 00:06:13,670 --> 00:06:17,570 Load S reads from a string and which one writes to a string. 100 00:06:17,570 --> 00:06:19,430 Here's how I think of it. 101 00:06:19,430 --> 00:06:22,970 The JSON format is a set of conventions for how a string 102 00:06:22,970 --> 00:06:26,900 should be structured so that it can be loaded into a Python object. 103 00:06:26,900 --> 00:06:33,755 Thus, Load S means load from a string or load a string into an object. 104 00:06:33,755 --> 00:06:37,700 Dump S means dump an object to a string. 105 00:06:37,700 --> 00:06:39,980 Dumping from a string wouldn't really make sense, 106 00:06:39,980 --> 00:06:42,030 so that's how I can keep track of it. 107 00:06:42,030 --> 00:06:44,370 Dump S, dump an object to a string. 108 00:06:44,370 --> 00:06:48,135 Load S, load an object from a string. 109 00:06:48,135 --> 00:06:50,610 That's the JSON format folks. 110 00:06:50,610 --> 00:06:52,700 We'll be seeing a lot more of it when we look at 111 00:06:52,700 --> 00:06:56,075 rest APIs that let us fetch data from servers on the internet, 112 00:06:56,075 --> 00:06:59,180 like from iTunes or OMDb. 113 00:06:59,180 --> 00:07:01,460 JSON dot Load S, 114 00:07:01,460 --> 00:07:02,890 loads from a string, 115 00:07:02,890 --> 00:07:04,275 into a Python object, 116 00:07:04,275 --> 00:07:05,850 either a list or a dictionary, 117 00:07:05,850 --> 00:07:10,640 and JSON dot dump S dumps from a Python list or dictionary into a string. 118 00:07:10,640 --> 00:07:12,530 JSON dot Dump S, 119 00:07:12,530 --> 00:07:14,045 with the indent parameter, 120 00:07:14,045 --> 00:07:16,210 yields a string with indentation, 121 00:07:16,210 --> 00:07:21,330 that makes it easier for people to read and understand. See you next time.