HOW TO: JSON and APIs in PYTHON - A Beginners Look

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Oh welcome John here in today's video we're going to be talking about and working through some examples using JSON data in Python and more specifically getting the data from an API cuz those two come quite mutually exclusive so what is Jason data jason stands for JavaScript object notation and is a data interchange format what that means is it's basically it's a human readable text form that is transferred as data it's structured similarly to Python dictionaries being that it has keys and values and a pair and it's used widely all over the internet and we're going to focus on it from one main person and one main purpose and that is working with an API as Python programmers is something we want to learn and use often to get data for our apps or web apps or maybe even write our own API so I open in front of me I have a JSON file and this is all fake data generated from a website so we can see straight away that the data is basically looks like it's contained within a list it's within square brackets and then within that each item if you like is within some curly braces and we can see here that we have some keys and values so we've got ID we've got age picture name company email and we've got here's one we've got tags and now this is a list and we have friends as well which is also a list with some more key and value pairs in it so what we're going to do is we're going to open this file up and we're going to access some of this information story and our own variables and and basically pull out the information that we want and save it so the first thing we want to do is import JSON now this is a module that's in the standard library so you don't need to worry about installing anything it's also worth noting at this point that when you use this module or any other ones that you don't use the specific name of the module in any of your files and it's quite easy to say load something something as Jason now that will cause you problems now I'll show you in a minute so the first thing we want to do is open our file now our file is in the same directory as our script and we're going to use a context manager good Python good programming practice so we're going to do Oh with open and then we're going to do file name which is Jason fake data very aptly named and we're going to call that as JSON file like that now what we're doing there is just opening the file with the context manager and storing it in this variable now we need to load in the JSON into a variable so we're going to do data it's equal to json dot load and then the json file okay so what that means is we're just loading it into our program and storing it all in the data variable so if we print out data now we'll get a nice long list of information there we go where is everything that's in that file so what we want to do now to access that is to index through it and pull out parts of the information that we're interested in and most so let's say we wanted to get the name so we could print data and now we want the very very first item which is here this is the first item in this list and within that we want the key of name so we would do name like that so it's a zero end in zero index language Python so zero is the first one if we print that out we should get the first person's name there we go now if we changes this to one we would then get the second person there let's change it back to zero and let's do the same for let's say the email data zero and then call the key which is school email like this except I've made a mess of my brackets so that's fix that there now that gives the name in the email and then let's say the balance as well that's so data balance like so print that out and now we've got the name email and the balance for this person so what if we wanted to store this information and do something with it so why would do is I would always create our own dictionary and we can call this person well I cannot type let's create our own dictionary like this and it's called the first key name and that's cool name and then email email and then balance balance like that so I'm a little bit ahead of myself because we haven't created our variables for that so let's remove our print statements but we know that this is the information that we want and we know that the name is here so let's do name is equal to what we just printed out there and then email will store that will store that at the date over that we're getting from the email key and then balance and then let's print out the person that we've just created and hopefully we should get all the information here right so now we've created our own dictionary and stored that information name the email and the balance so that's all well and good but what if we wanted to go through every person that's on this to in this JSON file now this one I think there's only about five or so one two three four five whereas your file might have loads more but you want to loop through it anyway to get all the information out so we're going to create a for loop within our context manager and we're going to do for item in data and then we're going to indent all of this into our for loop so what we're saying here is we're going through each item which is this this is one this is what we're going to call an item and we're going to go through our data which is where we've loaded our JSON file too and we're going to want to go and get this information create our person here and then store it in a list so when it first of all we need to create an empty list so we'll call this person list there we go now we need to change this from data indexing it first part when you change it to item because that is what we've decided to call each of these you can call this whatever you like I tend to call it item because it's just what I got used to we could call it person or something like that but we've already got quite a few things here called person so we'll ignore it and we'll just call it item for now and we'll change these ones as well item and name email address so well then we need to do person list not here let's remove this for a moment in don't back down here we're what now at the end but within our for loop we want to append this person to the list so we would want to do person list dot append which means to add and then our person so what we're going to do is we in a loop through each each each item here which is where the information is we're going to get the name email on the balance we're going to create a little dictionary with that and then we're going to add that dictionary to our list so if we've done this right we should get a list of dictionaries of each of the person so there we go there's that first one the second one third the fourth and the fifth okay so that's all well and good but what if we wanted to get the JSON data information from an API so I'm going to clear this off right now so we're gonna be making requests to a free and open API called the punc API this is from a beer brewer called brew dog and their API has information about all the beers that they make et cetera et cetera so the first thing that we always want to do when we're looking at a new API is we want to read the docs I so I've got the page open here in the documentation it tells us about the endpoint authentication and the rate limits so this is always important to make sure do you read this part when you are learning and working with an API so you don't flood it and end up banned or whatever but this one says that we have a rate limit of 3,600 per hour which is plenty for us and it talks about how to create your requests so definitely worth reading all of this and all of this information okay so we're going to go ahead and make our first request to this so the first thing that we that we want to do is we want to import Jason because that's the format that we're going to be working for in the file and we're also going to import requests which is a library that I use a lot in my videos for web scraping etc etc and it's what we're going to use to get the request from the server so let's set our URL which we want to be as the endpoint which was up here somewhere okay yep this but what we're going to do is we're actually going to work with the random one first so you can see here this will get us a random beer so let's put that in there get rid of that little space and then we want to do R is equal to request get URL and then load that into our data which is Jason load s in this case and then our dot text my web why we are sorry and why we doing load s is because we're going to be taking the string that comes back from our request in a text format I'm going to be loading it as a JSON object and saving into our data variable so if we were doing that where we did our file before we just did load because it was just loading from a file in this case it's loading from a string the our dot text string so we need load s so let's print out that and what we get back hopefully we get some good information there we go so here you can see all the information that came back from that API request to the random which should give us an one back so we can see you've got an ID we got a name tag line etc etc if we looked at the example response we can see that we've got our square brackets which is contains all of our information we got our curly brackets here which is our sort of main bit of info here and then a lot more within that which is basically what we've got back here so let's clear that off close this down okay so let's go ahead and access some of this information again like we did before so there's only one one sort of beer on this list so let's we have to use index zero to get the first one any other ones would be out of range and then that's called the name again I think that was a key called name there we go so we've got the name and it's going to be different every time because when we request information from this URL it gets us a random one so you see we've got a different one again a different one again if we go back to our response we can see what else can we get tag line tells you about it and ABV I think that's the alcohol content so let's do print that one and then we'll do it all on one line this time so we get it all in one line what I say tag line and then oops data so hopefully this will get us what I do wrong this here got my brackets in the wrong place okay there we go so that response has given us the name a little bit about it and the alcohol percentage another thing that's quite useful that we can do is that we can look here and it says you can specify your own parameters so if we look at this one it's using this here called brood before but what we're going to do is we are going to look at this one which is called food which returns all beers matching a supplied food string so if we go back down to our response right at the bottom here we have food pairing and it suggests three different types of that go well with this beer so what we're going to build is we're going to build a little command line program that will take an input from a user that than for food type and it will match any responses on the API for beers that go with that so to start doing that we're let's clear this off here we'll keep these two lines because they will remain the same and we are going to need to change this to what we had here and where is it gone parameters here we are so this one you can see after v2 and beers it's got a question mark the parameter name and then an equals so what we can do is I believe it's called food so if we change this and we do that and then we do food and we'll hard-code it for now and we'll just say PI and then print data like that just to double-check that we're actually getting a proper response nope you can see we've got lots of results back okay great so let's go ahead and do what we did before and get the first one and then the name and see what buy is great so we're starting to to get it to come out together here just print the length out and see how many we get back for that so if we do length of the data for pi mr. Brackett okay clear that off we can see right away that we have 25 responses from this API that beers that match the food of Pi and we can see down here in the documents that it says this performs using a fuzzy match now a fuzzy match is kind of like it returns all non exact matches of our keywords which gives us quite a good way of searching this field without having an exact string match which would be pretty tough to do because if you look at data that comes back imagine that having to type that in to get an exact string match to get the information back it wouldn't work right so now let's carry on and say we can get 25 responses that was just C we changed it to Berger let's see how many responses we get only 20 so this brewery thinks that more of its beers go well with pies and burgers it's quite interesting okay so now we want to do like what we did before let's create a list so if we create our list under hits call it a beer list now we're going to want to add all of the information for each beer that we get back from our API response to this list so we have a nice clean data and we don't have all of the stuff all the other data that we aren't interested in so let's do let's do for beer in data and we'll call it beer this time from our data so as I showed you in this one each one of these became item it's now going to be data beer sorry and data and we will do name it's equal to beer name I think that works and then that way other information tag line tag lines are useful one beer tag line and then ABV for any of us that may or may not want to consider drinking really high strength beer so if we were to do let's see if we've got the right tag so we'll do print name tag line and ABV hopefully we get a nice long list there we go so we should have all of the ones that match burger we've got the name the tag line and the strength okay so coming together so as with the person one let's create a beer item I'll do dictionary so then we'll call the key name and hopes for that back over there name and this is the name tag there that we've called and then tag line again line and we'll call this a B which is alcohol by volume just remembered that okay so now if we print out beer item we should get the same thing again but a bit more structured there we go name tag line and at ABV so let's append these as we did before to our beer list so let's do beer list dot append beer item and then underneath that we'll do print beer list it's clear laughs we don't get any conflicting there we go so now we've got our list of all the individual beers in here so that's a good start but now we want to be able to change this based on user input and get it to suggest one of the beers from the list and not all of them so let's take this off here and make our URL everything apart from the string that you want to query and keep all of that so the first part we want to do is let's call this food choice is equal to and we'll do input and let's call it let's just say please and your dinner choice and then that should do now we can use an F string here to put the variable in to the end of the URL for whatever the food choice ends up become ends up being now F strings are really useful for this so we can at the start of our program the user input will store whatever we put into the food choice variable and using the f and f string we come and the curly brackets here we can put that straight in at the end of our URL that works so if we run that we should get an A user prompt and if we do PI it's going to return all of the beers in a big long list that match the food pairing of Pi great so that works so now we need to get it to select a random beer from the list and suggest it to us now there's a couple of ways we can do that but what I'm going to do is I'm going to basically use the length of the list to pick out a random number for the item from that list and then use that so it's just going to select a random one and to do that we need to import random more specifically actually from random import Rand int now that will suggest select a random integer between two numbers okay so that after we've created our beer list we don't want to print it out because we don't need that so we want to basically get the length of our list of beers if you remember before one of them is 20 long one was 25 long depending on what we put in so let's call that value for now and do Rand int between 0 and the length of our beer list that should work okay and then we can create new variables for let's just say try this work is equal to beer oops list not item beer list of value so what this is going to do is basically going to set up our index of using value which is going to be between 1 sorry 0 0 value index and then the length of our beer list and it's going to put that value into here as the index so if we did print try this and we do this again that'd make this a bit bigger and let's just do pi this should give us 1 and 1 entry now if we do the same again we should get a different one see so what we need to do is we just need to get these specific information and print it in a nice nicer fashion but that's essentially using a random selection based on the criteria we put in for the food choice and that we've requested from our API so if we just finish this off now let's do try name is equal to try this because that's the variable we've stored it in and try tag line and the same for try this be like that and then now we just want to have a nice string to print out our suggestion and again we're going to be using f strings for that so let's do F you should try and then put in space try name and then tagline and we'll put in then another one ABV and then just add in the % at the end so it's nice and tidy so again we're using f strings to put in the variables into our print statement right so hopefully that works don't need that there yes I do get rid of that there we go that's better all right let's move up here type in not up there here PI ok you should try some punk ocean fermented lager 7.1% hmm maybe not run hick n burger should try nine-to-five wizard in D parallel wise in 7.5% perfect there we have it guys we've looked at JSON data structures we've opened JSON file and taken parts of the information out and created our own dictionary with that information we've queried a beer API to get random beer information and save that and then we've used f strings to manipulate the API URL to partake user input and then we've created a nice little fun command-line suggestion for what beer could go well with what food you're potentially eating so hopefully you found this useful and hopefully not too convoluted it's a bit longer this time if you got any questions let me know cheers guys bye
Info
Channel: John Watson Rooney
Views: 3,289
Rating: 4.9682541 out of 5
Keywords: python, learnpython, json data, python json, python apis, python api requests, python requests
Id: YgO5ff9sp7A
Channel Id: undefined
Length: 25min 40sec (1540 seconds)
Published: Wed Jan 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.