Got a YouTube account?

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

English subtitles

← JSON - Web Development

Get Embed Code
2 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Okay, the next thing I'd like to talk about is
  2. JSON. JSON serves the same purpose as XML, which is it's
  3. a nice kind of computer and human readable way to
  4. exchange data in a, in a consistent format. It stands for
  5. JavaScript Object Notation. The reason it says JavaScript is because
  6. JSON is actually valid JavaScript code. It might look something like
  7. this. You know, to use kind of our travel search example
  8. from before. We have kind of this dictionary structure. This actually
  9. looks a lot like a Python code, because Python and JavaScript have very
  10. similar syntax for dictionaries and lists. So,
  11. we have this dictionary. It might have
  12. a key called itineraries, who's value may be a list of other dictionaries. And
  13. in this case, we have a dictionary for each routing, or something like that.
  14. Or we have a dictionary for each leg, which may have, you know,
  15. a key for from, and a key for to, and a value for each
  16. of those. Now this might be, you know, leg 1, and this might be
  17. leg 2. And you can see, you know, leg 2 is also made up
  18. of another dictionary, which is what we use the curly
  19. braces for. Which has a couple key value pairs of
  20. it's own. You know, key from value IAD and key
  21. to value SEA. And we can close our list and we
  22. can close this dictionary. JSON is really handy for expressing
  23. these types of objects. Anything you can express in XML you
  24. can also express in JSON, except JSON is a little
  25. less verbose because you don't need these kind of opening and
  26. closing tags. You can build things up out of dictionaries or,
  27. you know, a mapping or an object depending on what vocabulary words
  28. you want to use, hash table, which is just a curly
  29. and then a list of key value pairs just like you would
  30. in Python, just like you would in JavaScript. You can also
  31. have lists, which are, use brackets just like Python does, and separate
  32. the values in the list with commas. So we can have 1,
  33. 2 and the string three. So we can have both integers and
  34. strings in our lists or in the value of a hash table.
  35. The list can also be the value of a hash table. A
  36. list can also be an item in a list. This could look
  37. something like this and so we've got a list inside a list here
  38. a with two more data types, a Boolean which you are familiar
  39. with true or false and a float. And these are basically all the
  40. data types we can have in Jason int, string, Boolean, and float.
  41. We can of course also have null which would be inside, for example,
  42. the empty list. And our main data structures are
  43. dictionaries or mapping, which is the key to a value
  44. or multiple keys to multiple values and lists. So, what
  45. I'd I like to show you now is how to
  46. parse JSON in Python. Okay, we're in our Python
  47. interpreter. We can import JSON which is, is now included
  48. in Python's version 2.6 and newer. If your using Python
  49. 2.5, I suggest you try to find 2.6 or 2.7.
  50. But I think app engine uses 2.7, which is what we've
  51. been using in this class, so you shouldn't have any problem importing
  52. JSON. If we were to make a JSON string in Python
  53. here, let's call it j. So, if we were to take a
  54. string to representing some JSON, in this case, it's basically a
  55. dictionary with two keys, "one" and "numbers", and the value for "one"
  56. is 1, and the value for "numbers" is the list [1, 2,
  57. 3.5]. Let's parse that in JSON. JSON we use the function load
  58. S which basically stands for load string, there's also load but
  59. that expects a file. And in this case, we're going to be
  60. using just load S. When we run that, we get back
  61. a Python dictionary. With our same keys, numbers and 1, you know
  62. the order doesn't matter in python dictionaries, and our same values,
  63. 1, and 1, 2 and 3.5. And so if we were to
  64. store that in a variable. D. We can manipulate it like
  65. this. We can look at d numbers and we get our list.
  66. We can look at d1 and we can see our number
  67. one. There's actually, because JSON looks just like Python, we could actually
  68. eval j, and what eval does is it actually treats this
  69. as Python code as if I had just typed this at the
  70. prompt. And this is the result we get. Now, that's a
  71. neat thing you can do. Never, ever do it. Because in addition
  72. to having, you know, valid JSON in here, somebody could actually
  73. have code that might, you know, do something to your computer. So,
  74. never use eval for parsing JSON. I just wanted
  75. to show that you can use eval to parse
  76. JSON. It's a really convenient thing when you're working
  77. in Python with JSON. The two sync up very nicely.