Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Dictionaries
Lecture: To and from JSON

Login or purchase this course to watch this video and the rest of the course contents.
0:01 When I introduced a section on dictionaries
0:02 I said that JSON and dictionaries are basically isomorphic,
0:05 there is a one to one mapping between these things.
0:08 Let's explore that relationship really quickly here.
0:11 So here is some text, just a single string, multiline string here,
0:17 and it's in the JSON format.
0:20 We'd like to take this, which looks extremely like a dictionary in Python,
0:23 in fact if I take this, say "print(type(movie_json), movie_json)"
0:31 and I run this, you'll see this is a string.
0:34 However, if I say "movie_dictionary = " just copy and paste out of there
0:40 and indent a little and I do the same thing,
0:42 this is actually a dictionary and it's pulled this value,
0:45 I mean, it's basically like you could take the text or JSON and evaluate it, almost, right.
0:52 So, that's really cool, but given this text, at runtime,
0:57 how do I dynamically turn this into JSON
0:59 because copy and paste static data doesn't make any sense,
1:02 so we are going to use the JSON module,
1:05 so "import json", spell correctly, we'll come down here,
1:08 let's put this down at the bottom for a minute,
1:10 we'll come over here and we'll say "movie_data ==" we just say "json."
1:18 Now notice there is a "load", which actually takes
1:21 a file pointer and a "loads", which loads from the string,
1:24 so we'll say movie_json, so let's just print this out here.
1:28 So we'll do something similar, we'll print the type of movie_data
1:31 and then let's print out movie_data itself.
1:34 So if we run it, we actually successfully parse this JSON text
1:38 which in reality probably comes somewhere off the web or off the file system,
1:42 we parse that into a dictionary and here is the dictionary printed back out.
1:46 So we could answer questions like "The title is {}.format()"
1:52 and we'll just say movie_data.get(title), boom. "The title is Johnny 5".
2:01 So, that's a super nice way to go from JSON into Python dictionaries,
2:05 what about the reverse?
2:06 So if I have some dictionary, let's just take the same data and reverse it,
2:10 we can say so let's just call it movie_json_text_2,
2:14 and we can get that by just saying "jason.dumps()" and if we give it a dictionary
2:20 it will dump that out as JSON.
2:24 So here and we can just print type of this thing and then print that.
2:30 Perfect, dictionary here is the dictionary data, used it to pull some data back,
2:34 and we want to go the other way to send it across the wire,
2:38 save it to a file system, we now have a string and it's actually the JSON text here.
2:43 So we used the JSON module to make this transformation
2:46 between JSON and dictionaries,
2:48 "loads" for strings, "load" for file pointers,
2:51 and then "dumps" for string, "dump" for file pointers.
2:56 So to review in a graphic, here we have "import json" so we can use that module,
3:00 we have string, which presumably comes off the internet
3:04 or off the file system, which is a set of JSON text that we want to parse,
3:09 so we can parse that into an in-memory dictionary saying "loads",
3:13 now movie_data is a dictionary, if we want to save it back to the file system
3:17 or otherwise treat it as a string, "json.dumps", boom, done.