How to Parse JSON in Python - JSON Tutorial for Python Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so in this video we are going to touch upon how to parse json i realized i never actually made a video dedicated to this before so now's the chance i mentioned this in my last video which i haven't released yet but that doesn't matter because this will come after that and where we took a python dictionary turned it into json and then wrote it to a json file and i mentioned i never made this kind of video so if you're interested and how do we parse json you know the basics of parsing at least then this will give you hopefully a good understanding to go forward with that so if you like this kind of stuff i talk about various different tips and tricks and how to's and programming and usually things i find on my own in my own work or just you know doing stuff on my own and then sharing it with you guys so if that sounds interesting don't forget to hit subscribe and let's just do a brief look at what json is i'm assuming you already know since you found this video and you're interested but if not it's just a text format for storing and transporting data as w3school says here and json stands for javascript object notation if you've seen a dictionary in python it looks a lot like it and here we go it has a property and a value and another property another value another property another value and just like a dictionary in python you can also have lists and your json and they will hold a collection of json objects for example this right here this fake api that i've used in previous videos that we've called with our angular app and here we have a list of json objects for different to-do list items so we'll get to how to go through a list here in a second but i want to start off pretty slow and just pull up one individual json object and then we'll take out the parts that we need so i guess another question you might have is why would i want to parse json and that's because json usually holds a lot of information like this right maybe we don't want every single to do maybe we just want to look at the fourth to do item so where id is equal to 4 and we want to know the title we don't care about the rest of this stuff we just want a particular piece of the json which is a real use case so that's what we're going to use parsing for and we're going to start with the first post here and i'll try to remember to link both of these urls in the description so if you're following along you can grab those links and do just that so let's copy this one and let's go to our python file and the first thing i want to do is import requests and also import json so these two modules we're going to use the request we're going to use to call that fake api and then the json we're going to use to load the response of that request and serialize it into json so let's create a variable called response and i'm going to say that is equal to requests.get and then we'll paste in the url and then next i'm going to do response underscore json this variable is going to be equal to json.loads and then what are we loading what are we turning into json we're turning the response dot text into json so now if i print out this response json what is it going to look like so let's run this pi test so here's the response it is exactly the same as what we see here now if you wanted to you can create a new file and save it as a json file and paste this response in here and then there are extensions here in vs code that you can see i was looking at it in the last video you can turn json into pretty print which they just format it for you where it looks a little better and it's easier to read than all in one line like it is here but we already know what it looks like because we have it right here for us so we're just going to use this as a reference so now that we have the response json what if we just want to grab a title well it's pretty easy i'm just going to print instead of the whole thing response json i'm going to say give me just the title and we're going to put square braces and then inside of the score braces we're going to put the string title because that is the property and then what's going to return to us it's going to return the value of that property so remember this is the value here of title so now let's save and run that here it is just giving us the value of the title property same with id if i wanted to get the id instead of title the property would be id and we'll run that here you can see id is one okay so that was simply you know one json object and we grabbed one of the properties values and the value was just a string it wasn't another json object which is typically what you will see you'll see something more in depth i guess like this and maybe we'll do this last something a little bit harder but before we do that let's move on to an array i called a list before but i guess the proper name is an array of a collection of json objects so this one's a good example of that here we have an array of different to-do's and let's go ahead and take that url and now we'll place it in this get and instead of response json id let's just grab the very first json object and so to do that we are just going to put a zero as the index here of this array right because that's what is this array and index 0 would be this json object index 1 would be this one so on so forth so let's clear this and run this test pie here you can see we got the very first one so if i change this to a one saved and ran that we now get the second to do item all right what if we want to loop through all of the to do items and we want to say well if id is equal to 5 get me that title that's a good example so let's do that so i'm going to kill this print and i'm going to say for to do and response json if to do id the property id if its value is equal to what i say five i don't remember now but let's just say five print the to do's title so what is this doing well this first part this 4 it's going through each json object in this array so it's starting off with this one and then going down down the line and then it's saying for each one of these let's check the id so if the to do id is equal to five okay if that passes which it would pass at this one then we'll print out the title of that json object so let's run this and see that it works let's look this should start with this word right here and let's see if it in fact does that here it goes so loop through them it ignored the first four because the id was not equal to five but on the fifth one it was so then it printed the title it continued on through none of the other ones ids were equal to five so this is the only one that it ended up printing but it did in fact loop through every single one of these in the array okay so i said we'd do maybe a harder one and i found this on the same fake api website that we've been using this users it's a little more uh intense i would say because now we have a list and we can grab the very first user here and then we see address is actually its value is another json object and then inside of that this geo property its value is another json object so there's multi tiers to this and here let's just look at the first one so i wonder if i put slash one yeah we're able just to get the first one so let's do that let's just look at the first one here i'll copy its url place it in this and let's think about this so id is pretty self-explanatory we just went over how to get it but let's go ahead and let's say we want to get the street that this person lives on how would we do that so first thing we would do is we want the value of address right and the value of address is this entire json object and then inside of that json object we want the value of street so it's actually pretty simple what that would end up looking like let's get rid of all this is print actually let's do it outside of the print let's say street this variable street is going to be equal to response json the first property that we want to get the value for is address so let's copy address and let's say give me that okay but inside of address we also want the value for street so we're going to put another square brace right here and inside of that put street and then below that let's go ahead and print the value for street let's see what that looks like clear this and run coolest light is that what it in fact is yes and then if we wanted lat we would go another one further so instead of street would be geo and then another square brace and lat so let's run that and here's the latitude negative 37 degrees or i guess that's a number not a degree negative 37. maybe that is a degree i don't know and then the same could be said with the name of the company so if we wanted the name of the company we would first get the value of company and then the value of name so let's just do that company and then name and we don't want latitude any longer and instead of street let's say comp for company paste that in there and run this and here is the company's name so that's how you can parse out a more intense i guess example of a json object so i hope this was helpful i hope it at least gets you started with parsing json you're able to take the json that you're wanting to parse and mess with it and get the pieces out of it that you care about and hopefully uh you stick around for more hope to see you in the future and take care
Info
Channel: Coding Under Pressure
Views: 35,582
Rating: undefined out of 5
Keywords: parse json in python, how to parse json, how to parse json in python, python json, json in python for beginners, python tutorial, json in python, json tutorial
Id: SNv7E-cXCu0
Channel Id: undefined
Length: 11min 43sec (703 seconds)
Published: Sat Nov 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.