Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← Parsing JSON Solution - Web Development

Get Embed Code
2 Languages

Showing Revision 4 created 05/24/2016 by Udacity Robot.

  1. Okay, I'm going to show you how I arrived at my answer. So

  2. I'm going to select this line. And I'm actually going to use the Python
  3. IDE, to, to figure out the kind of structure of this document.
  4. Here we are in the IDE. I'm going to paste in that first line.
  5. So I pasted in that first line. It's in a variable called
  6. reddit_front. We can go ahead, and run length on it. We see
  7. it's 26,000 characters. Okay, so the first thing I'm going to do is
  8. import json. And then I'm going to convert this, this document into json
  9. using the loads function in the json module. So now I've got
  10. this big dictionary: j. And it's got all this stuff in it. Actually,
  11. that wasn't very useful. It just printed everything. So let's look at j.keys.
  12. We can see there are two keys here: kind and data. Data is
  13. almost certainly the one we want. Let's look at that. Oh, another
  14. bunch of stuff. Let's look at the keys on this. This has just
  15. four keys: after, before, children, and modhash. Children is going to be one
  16. we want. The other ones are just simple little variables. So let's look
  17. at children. Now we're starting to get somewhere. Let's look at the keys
  18. of this. It's a list. So it's probably a list of lengths. Which
  19. is kind of what we're expecting. Let's look at one of these. Again,
  20. a bunch of crap. But let's look at the keys for the first
  21. element in the links, or in this children's list. We can see that
  22. it has kind and data. So let's look at the data for this
  23. guy. We're starting to get a little bit closer. Let's see what the
  24. keys are for this guy. Aha, perfect. And we can see that 'ups' is
  25. actually in this. So if I were to call ups, we can see that
  26. it is the integer of the number of ups on this link. So that's how
  27. I found this. So, looking at our total JSON document, were going to look at
  28. data, were going to look at children. And
  29. then for each of the children, were going to
  30. sum up the ups. If I were to change zero to one, to find
  31. the second element on the list, we can see that we get another variable. I'm
  32. going to take this piece of code with me into the ide, and were going to write
  33. a function to add up all the ups. Okay, here we are in the ide,
  34. and what we want to do is we want to sum up all
  35. of the ops. So I can say sum. So I'm going to
  36. say C data ups, for C in data children. Basically what
  37. I'm doing, is I'm iterating over the list data children, which
  38. we know is a list. For C, and each element in
  39. that list, I'm going to look up data ups on that object
  40. C. And then we're going to sum it up using the Python
  41. built in function, sum, and I'm just going to return that.
  42. Let's give that a run. J is not defined. That's
  43. means I didn't load the actual string of the Reddit
  44. frontpage in JSON, into a JSON object. So let's do
  45. that. Let's run that again. Here we go. Now, here's your
  46. answer, 103978. Simple enough. Now, what I wanted you to
  47. accomplish there was just learn how to you now load this
  48. into JSON, and then manipulate the data structure a little
  49. bit. And, you can see it's just like manipulating any Python
  50. data structure because JSON maps very cleanly to what we
  51. already have been working with in Python which is dictionaries
  52. and lists and integers and floats and that sort of
  53. thing, so. Pretty handy there. You are now a JSON expert.