1 00:00:00,700 --> 00:00:02,930 - [Instructor] To finish up our introduction to 2 00:00:02,930 --> 00:00:05,840 json processing, let's take a look at how you 3 00:00:05,840 --> 00:00:09,310 can print out formatted JavaScript 4 00:00:09,310 --> 00:00:13,110 object notation data so that it's easier to read. 5 00:00:13,110 --> 00:00:15,540 So, if you look back up here, in snippet four, 6 00:00:15,540 --> 00:00:18,970 where we simply listed out the contents of the json file, 7 00:00:18,970 --> 00:00:22,550 it just came out as one long line of information. 8 00:00:22,550 --> 00:00:23,980 But, it would be nice to be able to 9 00:00:23,980 --> 00:00:27,570 read this in a more structured representation. 10 00:00:27,570 --> 00:00:31,610 So, it turns out there is a function in the json module, 11 00:00:31,610 --> 00:00:35,103 called dumps, which stands for dump string. 12 00:00:36,140 --> 00:00:39,980 We're going to use that here to produce a formatted output. 13 00:00:39,980 --> 00:00:42,610 I'll execute it first just so you can see. 14 00:00:42,610 --> 00:00:46,200 And now let's talk about what produced this output. 15 00:00:46,200 --> 00:00:49,340 So, first of all, we opened up the json file that 16 00:00:49,340 --> 00:00:51,620 we wrote to previously for reading and 17 00:00:51,620 --> 00:00:53,642 we're going to call that accounts. 18 00:00:53,642 --> 00:00:58,642 In our print statement here, we have a nested function call. 19 00:00:58,700 --> 00:01:02,120 First, we go to that file called 20 00:01:02,120 --> 00:01:04,910 accounts and we load it up in to memory. 21 00:01:04,910 --> 00:01:08,980 That actually is turning back into the Python object. 22 00:01:08,980 --> 00:01:11,540 Now, we're taking that Python object and 23 00:01:11,540 --> 00:01:15,440 dumping that string of information here in the 24 00:01:15,440 --> 00:01:20,050 context of the Python interactive environment. 25 00:01:20,050 --> 00:01:24,970 Now, the dumps function takes as its first argument an 26 00:01:24,970 --> 00:01:28,290 object that we want to see the string representation of 27 00:01:28,290 --> 00:01:33,170 in json format and the second argument is the indent that 28 00:01:33,170 --> 00:01:36,902 you'd like to use for nesting the contents of the 29 00:01:36,902 --> 00:01:41,300 JavaScript object notation object that's being output. 30 00:01:41,300 --> 00:01:43,740 So, we're using four space indent. 31 00:01:43,740 --> 00:01:48,740 Now, if we look at the original output here in snippet four, 32 00:01:50,410 --> 00:01:55,410 the outer set of curly braces is at the very left edge. 33 00:01:56,000 --> 00:01:59,190 Within that outer set of curly braces, we had the 34 00:01:59,190 --> 00:02:04,190 accounts key and associated with that was a list of values. 35 00:02:04,890 --> 00:02:06,070 So let's look at that. 36 00:02:06,070 --> 00:02:08,130 So here's the accounts key and here's the 37 00:02:08,130 --> 00:02:11,410 beginning of the list and here's the end of the list, 38 00:02:11,410 --> 00:02:15,020 aligned underneath the accounts key and all of the 39 00:02:15,020 --> 00:02:17,380 list items are indented within it. 40 00:02:17,380 --> 00:02:19,350 And as you recall, there were two 41 00:02:19,350 --> 00:02:24,095 dictionary objects inside of there which were 42 00:02:24,095 --> 00:02:28,470 turned into Python, I'm sorry, json objects. 43 00:02:28,470 --> 00:02:33,455 These are those two json objects with their key value pairs, 44 00:02:33,455 --> 00:02:37,810 nicely structured so that you can see them indented 45 00:02:37,810 --> 00:02:41,640 underneath, inside of rather, the list in this case. 46 00:02:41,640 --> 00:02:44,710 So, this first set of curly braces represents the 47 00:02:44,710 --> 00:02:48,310 first json object then we have a comma and 48 00:02:48,310 --> 00:02:52,400 then the second json object in that list of information. 49 00:02:52,400 --> 00:02:56,540 So, by using the dumps function, you can get a nice, 50 00:02:56,540 --> 00:03:00,596 structured, readable string representation 51 00:03:00,596 --> 00:03:03,360 of the JavaScript object notation. 52 00:03:03,360 --> 00:03:06,010 Obviously this is not as compact as 53 00:03:06,010 --> 00:03:10,564 what we saw back here in snippet four but it 54 00:03:10,564 --> 00:03:13,450 does make it a hell of a lot more readable. 55 00:03:13,450 --> 00:03:15,430 And when you're first working with 56 00:03:15,430 --> 00:03:19,240 JavaScript object notation, this dumps function can come in 57 00:03:19,240 --> 00:03:23,030 particularly handy for helping you understand the 58 00:03:23,030 --> 00:03:26,700 structure of the JavaScript object notation that you're 59 00:03:26,700 --> 00:03:30,560 either creating or if you're receiving it from a web service 60 00:03:30,560 --> 00:03:34,153 perhaps, that you're receiving into your application.