How To Convert JSON To CSV In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome this is black lotus your youtube script kitty and this is how to convert a json file to csv using python so i have a json file that i generated from macaroon.com it's a bunch of fake random data and we'll use python to convert that into a csv so first thing i'm going to need is to import a couple modules specifically the json module and the csv module and in my program i also want to specify the file path to the json file so i'm going to need sys at the very least and i'll temporarily just call it file path and give it an r give and i'll set up a try bot for now without defining the function just yet and i want a file not found error okay and within the char block i will set up a context manager to open the json file and i'll load in the data and i'll just call it data okay and since the json file has been formatted to have the exact same fields uh that are the exact same keys for each json object i can just take the first json object and use the keys for that as my csv headers so i'll go ahead and do that i'll declare variable called headers i'll grab the first record and just grab the keys from that okay next i want to set up a csv file so i'll start another contacts manager and we'll call this one csv file and we'll just give it a name a temporary placeholder name for now in write mode and csv file and to write to a csv we need to set up a writer so we'll call this writer csv.writer and we'll pass in our file name and the first thing we'll want to do is write the headers so write our dot right row and we'll pass in the headers and finally for each additional record in the json file i want to generate a row based on the record and input that into the csv file so for record and data um we'll just call it a record and then we'll set up a generator expression and then write each one of those expressions to the csv file so we'll do it for there so i want to now define a function and then separate that function from the rest of the program so we'll set up the separation first and here i want to what do i want to do here i want to make sure that the file name actually exists within the within my computer so i'm going to move the file arguments down into the action down into the main function here and i also want to pass in to the terminal a csv name as defined within the function okay and since i don't have no longer have cis above our methods for it i'll just go ahead and import it down here i will move the try block as well and i'll set up the definition up here so we'll call this json.csv pass in the file path and the csv name okay um that would work and one additional thing i'd like to do is to actually check if the file is there and then terminate early so we'll use path live for that and and let's see if an argument is given and is not found it will or if an argument is not given within cis it does not raise a file not founder it will raise a index error change this to index error and we'll go ahead and exit it early and we'll just say two arguments required one json path and one save file name okay and if an exception does not occur we'll check if the file is there so pathlib.path is a has it both a enter and an exit method so we'll set up a context manager for this one as well i will say with pathlib dot path and we'll pass in the file path and as because this would also be json file to make it a little bit easier to distinguish this one from the one in the function we'll say we'll just call this one global this one up here local okay and within this context manager i will say if json file global dot is file and if it is a file we'll invoke json2 csv else we'll go ahead and print out this message down here uh instead of just using a print function we'll say sys.exit as well okay just double checking to make sure everything looks good uh hello spreader okay that looks good that should work let's give it a go json to csv so we'll call python3 we'll json to csv dot pi we'll specify the file path to the json file and this will be mock data dot json and then for this one we'll say oh i don't know test test from json dot csv and it's a syntax error all right that ran and here is the csv file i have my headers and everything looks good we'll just double check uh the first actual record is from the fictional character daisy dilru and the last one will be berta casellas and we'll go back to the mock data just to make sure daisy delroo and if we scroll all the way down so that did work okay that's how to convert a csv or json file to a csv here is the code thank you and i hope you got something out of it
Info
Channel: Black Lotus
Views: 5,436
Rating: undefined out of 5
Keywords: Python, Python3, CSV, JSON, Data Science, Coding, Programming, VS Code, Visual Studio Code
Id: LBB8i4s_jzw
Channel Id: undefined
Length: 10min 51sec (651 seconds)
Published: Sun Nov 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.