YouTube

Got a YouTube account?

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

English subtitles

← JSON Escaping - Web Development

Get Embed Code
2 Languages

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

  1. But you remember when we were learning HTML and we had to

  2. escape our HTML content so it renders appropriately in the browser? If
  3. not, we covered that in unit two I believe. JSON has, has
  4. a similar issue. So, you know, if we have this little JSON
  5. blob, you have a little dictionary that takes the key story and
  6. maps it to the string once upon a time. You know, this
  7. is valid JSON. Our key is surrounded by double quotes and our
  8. value is surrounded by double quotes. What if we want to include double
  9. quotes in our value? Now if I were to just put a
  10. double quote in here, this would cause an invalid JSON because this
  11. quote actually ends the string, and then we've got a bunch of
  12. garbage after the string. You know, we'd need a comma and another opening
  13. curly brace. You know, all sorts of things. That totally screwed things
  14. up. Let's see what would happen if we tried to use that in
  15. our terminal real quick. So I'm going to take that JSON string
  16. that we just loaded. So this actually works. I'm using our Python string
  17. is actually this whole piece. This is the, this is the
  18. JSON string we're sending in to Python, and it's surrounded by single
  19. quotes. Remember in Python you can use either single quotes or
  20. double quotes to delineate a string, and that's what we're going to
  21. use here. We're going to use single quotes so we can
  22. use double quotes inside the JSON, and that works just fine. And
  23. if I were to do what I just did in the editor,
  24. and replace this p with a quote, let's see what happens. Explosion.
  25. You know, the, the, the, JSON parser didn't like that at
  26. all. The way we get around that is we escape this
  27. quote by putting a slash in front of it. This escapes
  28. the quote in Python, but what this turns into is still basically
  29. the same string we had before, which is just the quote.
  30. I actually have to escape both the slash and the quote
  31. for this to work in Python. So, basically this slash is
  32. the Python escape for this slash, which basically says we are inserting
  33. a slash in the string, and yes, we mean to do that. The
  34. JSON interpreter will see that slash. And say, okay, they must mean to include
  35. this quote. And let's give this a run. There we go. The other way
  36. to do this in Python that's a little simpler, instead of using double slashes,
  37. which is kind of confusing, is, we could put an r in front
  38. of our string, which says, this is a raw string, which basically means, Python,
  39. ignore any escaping we're doing in here for the purposes of Python, and let
  40. the json module interpret this slash however it will. And if we run this,
  41. it also works, with our quote in the value. The answer as
  42. we just saw, if want to include a quote in our value,
  43. is to input a slash in front of it. And that's the
  44. u that we had there before. So we have to escape our quotes
  45. with a slash. This is not a thing if you want to read
  46. in JSON, because we assume that the JSON we're going to be reading
  47. is valid, and if its not valid, our JSON module will tell
  48. us, it, it will throw an exception when we try to read it.