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:04 I said that json and dictionaries are basically isomorphic,
0:07 there is a one to one mapping between these things.
0:09 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 the (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
0:39 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:51 So, that's really cool, but given this text, at runtime,
0:56 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 load s 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 fie 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:08 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.dump s 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:49 load s for strings, load for file pointers,
2:51 and then dump s 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:05 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 load s,
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.