1 00:00:00,690 --> 00:00:04,930 CSVs were and still are a very popular format for importing and 2 00:00:04,930 --> 00:00:08,310 exporting data to and from databases and spreadsheets. 3 00:00:08,310 --> 00:00:10,620 Much of the web though, runs on another common and 4 00:00:10,620 --> 00:00:14,980 popular format, JSON, or JavaScript Object Notation. 5 00:00:14,980 --> 00:00:16,930 No, I'm not trying to get you to write JavaScript, 6 00:00:18,370 --> 00:00:23,380 Python's JSON module is very handy for encoding and decoding data from JSON. 7 00:00:23,380 --> 00:00:26,360 You'll use this when you're receiving information in your API or 8 00:00:26,360 --> 00:00:30,110 sending information to an API, or maybe you just want to use it to read and 9 00:00:30,110 --> 00:00:31,800 write files on your server. 10 00:00:31,800 --> 00:00:32,880 JSON is pretty common for 11 00:00:32,880 --> 00:00:36,640 configuration files too, regardless of how you're going to use it, 12 00:00:36,640 --> 00:00:39,870 it's a great format to know about, and a great library to know how to use. 13 00:00:41,170 --> 00:00:45,520 Python's JSON library is pretty solid and I think, pretty easy to use. 14 00:00:45,520 --> 00:00:49,020 Let's check out this JSON file about a piece of art 15 00:00:49,020 --> 00:00:52,560 that I downloaded from the Minneapolis Institute of Art. 16 00:00:52,560 --> 00:00:56,500 You can see that a lot of stuff in here, I can read it like it's a dictionary and 17 00:00:56,500 --> 00:01:00,440 go, it's dated 1899, cool, but I don't wanna do that. 18 00:01:00,440 --> 00:01:03,560 I wanna be able to do this in Python, right? 19 00:01:03,560 --> 00:01:09,250 So I'm gonna make a new file and I'm gonna call this art.py, 20 00:01:09,250 --> 00:01:11,790 and inside of here, first things first. 21 00:01:11,790 --> 00:01:14,367 We need to import the JSON module, so 22 00:01:14,367 --> 00:01:18,119 the JSON module has all of our handy dandy stuff in it. 23 00:01:18,119 --> 00:01:20,774 So I'm going to open it up, what is this? 24 00:01:20,774 --> 00:01:24,750 148.json as art file. 25 00:01:25,940 --> 00:01:32,967 So I'm gonna open that up and I'm gonna say art = jason.load(artfile), 26 00:01:32,967 --> 00:01:38,281 and then I'm gonna print(art['description']). 27 00:01:38,281 --> 00:01:40,048 All right, cool? 28 00:01:40,048 --> 00:01:41,611 So let's go run this. 29 00:01:45,750 --> 00:01:49,447 And I get that it's a boat with two masts in front of a bridge with a lift-gate, 30 00:01:49,447 --> 00:01:52,640 two row boats in water at left, buildings at left, trees at right, 31 00:01:52,640 --> 00:01:55,270 that's the description I got from the JSON file. 32 00:01:55,270 --> 00:01:58,780 Now that might not look like much but it's really handy if you're dealing with tons 33 00:01:58,780 --> 00:02:02,920 of input from an outside source, or lots and lots and lots of files. 34 00:02:02,920 --> 00:02:08,010 And you don't have to try and turn plain-text files in Python Objects or 35 00:02:08,010 --> 00:02:12,852 anything like that through RegX or some sort of, I don't even know, 36 00:02:12,852 --> 00:02:15,279 some sort of arcane incantation. 37 00:02:15,279 --> 00:02:21,461 The load method, which we used right here, takes a file-like object and 38 00:02:21,461 --> 00:02:26,450 tries to turn that into a Python object of some kind. 39 00:02:26,450 --> 00:02:31,900 In this case, because what we have here is what we would call a dictionary in Python, 40 00:02:31,900 --> 00:02:35,940 it just creates a dictionary, it creates an actual Python dictionary. 41 00:02:35,940 --> 00:02:41,230 If we had a list, an array for JavaScript, then we would get a list 42 00:02:41,230 --> 00:02:45,650 in Python, another way that we can do this is with the load s method. 43 00:02:45,650 --> 00:02:48,990 You'll probably hear people call it loads instead of load s or 44 00:02:48,990 --> 00:02:52,270 load string, I call it loads all the time. 45 00:02:52,270 --> 00:02:56,690 Honestly, that's fine, that's what the word is, but the way that it's spelled, 46 00:02:56,690 --> 00:03:01,990 what it is, is it's the way that we can take a string with embedded JSON in it and 47 00:03:01,990 --> 00:03:04,030 turn that into a Python object. 48 00:03:04,030 --> 00:03:06,300 Let's try that out, let's try it out down here in the shell. 49 00:03:06,300 --> 00:03:10,484 We'll do Python, import json and 50 00:03:10,484 --> 00:03:14,524 let's do nums = json.loads and 51 00:03:14,524 --> 00:03:18,579 then a string of 1,2, and 3. 52 00:03:18,579 --> 00:03:24,009 All right, and so now if I do nums[2] I get the number 3, 53 00:03:24,009 --> 00:03:27,490 so we loaded the string in the JSON. 54 00:03:27,490 --> 00:03:31,270 In that string there was an array of integers which the JSON module turned 55 00:03:31,270 --> 00:03:36,670 into a list of ints and I think that's pretty handy. 56 00:03:36,670 --> 00:03:41,159 The opposite of load and loads are the dump and 57 00:03:41,159 --> 00:03:45,187 dumps methods, so let's try them out. 58 00:03:45,187 --> 00:03:52,312 So we can do json.dumps([5, 4, 3, 2, 1]). 59 00:03:52,312 --> 00:03:57,386 No quote marks, and now, I get a string which has the array of numbers in it, 60 00:03:57,386 --> 00:04:00,795 so it's kinda like I turned the list into a string, 61 00:04:00,795 --> 00:04:05,930 I mean, it almost seems like I get to str and then the list, right? 62 00:04:05,930 --> 00:04:08,760 And I could on something this simple, but on something that's a little bit 63 00:04:08,760 --> 00:04:11,750 more complicated, you obviously can't do that, let's try this. 64 00:04:11,750 --> 00:04:17,138 I'm gonna make myself into a dictionary, so first_name is Kenenth, 65 00:04:17,138 --> 00:04:24,140 last_name is Love, and my topic here at Treehouse Is Python. 66 00:04:24,140 --> 00:04:28,000 All right, so I can't just do str(me), 67 00:04:28,000 --> 00:04:33,670 I mean, I can but I don't get exactly the right thing, right? 68 00:04:33,670 --> 00:04:37,830 So let's do json.dumps(me) and 69 00:04:37,830 --> 00:04:40,700 while it looks the same, there is a difference because in JSON 70 00:04:40,700 --> 00:04:45,010 you have to have double quotes instead of single quotes to make it valid. 71 00:04:45,010 --> 00:04:49,780 So this is valid JSON, what I did before is not, and if I was to have 72 00:04:49,780 --> 00:04:52,860 numbers like that in there, we'd want to change how those things worked. 73 00:04:52,860 --> 00:04:57,340 We could also easily send this string out across the internet with no problems, so 74 00:04:57,340 --> 00:04:57,860 that's great, 75 00:04:57,860 --> 00:05:02,810 that's super handy, we could do something similarly using the Dump method. 76 00:05:02,810 --> 00:05:08,153 But Dump goes directly into a file-like object instead of into a string, 77 00:05:08,153 --> 00:05:11,540 so we have me, let's make Craig. 78 00:05:11,540 --> 00:05:16,707 So again, do first_name as Craig, 79 00:05:16,707 --> 00:05:24,170 last_name as Dennis, and topic as Java all right? 80 00:05:24,170 --> 00:05:28,455 So we have me, and we have Craig, and let's do with open 81 00:05:28,455 --> 00:05:34,650 teachers.json, and a for append, although it doesn't really matter, 82 00:05:34,650 --> 00:05:38,440 cuz we're writing into this for the first time, as teacherfile. 83 00:05:39,930 --> 00:05:43,922 Then we wanna say json.dump, and 84 00:05:43,922 --> 00:05:47,913 we're gonna dump an array of me and 85 00:05:47,913 --> 00:05:52,355 craig into teacherfile, all right? 86 00:05:52,355 --> 00:06:00,200 Let that run and now, let's shrink that down, refresh this, 87 00:06:02,800 --> 00:06:06,390 if we checked out teachers.json that the file that we just created. 88 00:06:08,690 --> 00:06:12,400 There's me, and I misspelled my own name, and 89 00:06:12,400 --> 00:06:17,880 there's Craig, and it made a list of objects. 90 00:06:17,880 --> 00:06:22,560 So that's pretty cool, that's super awesome, the JSON module is amazingly 91 00:06:22,560 --> 00:06:27,640 handy for both encoding and decoding data to and from files and strings. 92 00:06:27,640 --> 00:06:30,603 I find myself using it a lot with various APIs on the web, and 93 00:06:30,603 --> 00:06:32,393 I bet that you will find it the same.