[MUSIC] Hello everybody, welcome to Python for Everybody. We're playing with the web services chapter right now. And if you want to get the materials for this course, you can go here and download the sample zip, samplecode.zip. I've got this all sitting already on my computer. I also have the whole thing in GitHub if you want to get it out of GitHub. So the thing we're talking about now is talking about the json1.py example from the book. So JSON is kind of like XML except a lot simpler and that's why a lot of people like it. It's not that JSON is always better, but JSON is better in a lot of situations that don't require the complexity of XML. So we start to import JSON, JSON is built into Python, but we have to ask to import it. Again, we're using a triple-coded string to put the JSON in there. And JSON looks a lot like Python dictionaries, key value pairs. Key value pairs, in this case, this is a key and the value itself is another dictionary, or in JSON terms, an object. But again, key value pairs, within key pairs values, within key value pairs. And all these little cursor guides have to, all these little curly brace guids have to line up properly. And so like all the time, this is a string which we normally would read and decode from the Internet. But for now, we're just going to have it in there load json.loads. That's going to the JSON library, pull out load string. And parse this, which turns this set of curly braces, spaces, commas, and perhaps syntax errors, into a structured object. And if we had made a syntax error in here, then this would blow up. But if this doesn't make a syntax error, if this doesn't blow up, then we have a structured representation. Now, the difference between XML and JSON is that this turns into a Python dictionary, with key value pairs, okay? And so once we have this, this is a dictionary And we can say info sub name. And that's the exact syntax that we would use to get the dictionary. And that's going to extract this value out of there. And if we want to go in deeper, we can say info sub email and that is what info sub email is right there. And then sub hide, so that's a dictionary within a dictionary and so if we run this, python3 json1.py it digs in really fast. And so this is why people tend to like JSON is because you read the JSON which is actually a syntax derived from JavaScript, when it looked just like the syntax for Python. So, that's moving an object, a JSON object that turns directly into a Python dictionary with nested dictionary. So now we're going to look at json2 and so json2 we're going to see a list. An array and JSON terms but it turns into a list of Python terms. So this is a list of dictionaries, in JavaScript that would be an array of objects. But in Python it's a list of dictionaries. So we'll just pretend that it's a list of dictionaries. Again, we load the string, parsing, looking for syntax errors. So let's just make a syntax error here and run our python json2.py. And you'll see where it blows up. It blows up at line 15, which is right here, it's like this loads blows up. Now, you could put a try accept around it to save it but we're not going to do that. And it even complains it says, look we're expecting something here In the line 11, its the line 11 of the JSON which starts at line 4. And so I put my little square brace back in so its not syntactically broken. So, let's run it again and make sure that she runs and yes she does. So, this parses and converts from the JSON syntax into a python in this case list. Because it's got square braces instead of curly braces. The previous example had square braces and we can then take a line of it and it's an array, it's a list, and we see that there are two things in there. And then we're going to iterate through and this item is going to iterate through these dictionaries, that dictionary followed by that dictionary. So the first time, it's item sub name, which is this value right here, and then item sub id, which is this value. So you can dig right into this, but you're not using get, and you're not using the weird extra find, or find all, or anything you just are going at these structures directly. And so you can quickly extract this stuff out and we read through id's, name is Chuck, oops, name is Chuck. There are no attributes, by the way, x is 2, and so we had to make x. So if you look at the XML, we had this concept of attributes on the outer tag. These things are also not named, we just have to know what we're looking for. So JSON represents simple structures, but it's much simpler to use. So I hope this has been useful to you and talked to you in a bit about some more JSON. [MUSIC]