Python Part 16c - Linking to JSON files Office

Python Part 16c - Linking to JSON files Office Welcome to this weizel tutorial on working with json files in python so here's what you'll learn during your tutorial we'll begin by looking at what json is.

With some realistic examples and then we'll go on to look at how you can write information out to json files called dumping it and how you can read it back in and that's called loading it.

We'll look at how you can use dumps and loads to read and write to json strings and then finally we'll do a longer case study or example on working with the debug settings file to show how you can.

Get it information in a json file at any point you'll be able to click on the link at the top right of the screen to download any files or exercises to do with this tutorial.

And i think that's probably enough of me so i'm going to vanish now as i tend to do at this point in a tutorial and hand control to over control to sven he will reliably guide you through the rest of.

The tutorial so let's get started so we'll begin by looking at what a json file is json stands for javascript object notation and is a way of storing.

Python Part 16c - Linking to JSON files

Information in the text format it's exactly analogous to the old xml format it's just really replace that so what i wanted to do is look at four examples of json files just to give you.

An idea what they can can contain so this is a visual studio code settings file i'm actually looking at it in a different application so don't worry about that but you can recognize that.

It's a dictionary of settings for all the different things you can do in visual studio code so for example it says the color theme to visual studio lite.

Another example might be the debug launch file in visual studio code again this is stored in json text format another completely different example might be a map file of all the counties.

In the united kingdom and this stores map points of every single county and it's quite a long file as you might imagine it's nearly a hundred thousand rows.

And another example might be a theme for using in power bi desktop and this controls how by default all the visuals you create will look so you can see the json.

    Text files can contain almost anything

    I don't know if it's used for an analogy but let's look at this in terms of star trek the process of creating.

    Writing to and reading from a json file it's called serialization and deserialization and you can think of it as something like this.

    So you have the crew of the star tech star trek enterprise sitting waiting to visit a different planet they are like a collection of information on maps or visual studio code settings or power bi.

    Themes or whatever maybe you want to transmit to somebody else or some other computer program to do that you need to convert the live objects into basically zeros and ones or.

    Bits and in star trek that's done using a transformer i think that's what it's called i've a horrible feeling i'm going to get lots of people complaining about.

    The fact i haven't described what happens in star trek faithfully please don't because that's not really the point of this tutorial so.

    The star trek uh transformer is equivalent to a jace writing a json file and what that does is translates the maps and code settings and themes or whatever maybe into a text readable.

    Format similar to the ones we've just looked at at the other end the star trek voyages get transformed back i think.

    Actually they've been transformed into completely different people but that's unfortunate uh but they get transformed from their zeros and ones back into real people and likewise you can read a json.

    File and convert them back into usable

    Maps or settings or themes or whatever it may be so that's what's going on when you write and read from a json file.

    So i've created a file called writing jsonfile.py and what we're going to do first is use that to write out or dump out a text to a json file.

    In order to be able to write information i need to have something to write so what i'm going to do is delve through my clipboard and see if i can find something and happily i seem to have a.

    List of harry potter's characters there uh the first name the last name the house and i'm not quite sure what that fourth field will be surely it can't be a rating hermione.

    Deserves more than that so what we're now going to do is write that to a json file and to do that we need to import the json module and like with csv it's a built-in module.

    There's no need to install anything so that's good what i'm now going to do is open up a file as is so often the case it's like say with open.

    I'll put in an r to make sure i don't have to escape my backspace characters and then i will paste in the content of the clipboard actually i won't i'll paste in something previously i've used.

    In the clipboard and that will give me the file path i need to specify one more thing i need to say i'm writing to it rather than reading from it which would have been.

    The default and the w will mean it's overwritten every time i run the program a would have appended to it so i can call that json file and then i can dump out the.

    Harry potter info and i've chosen the word dump quite advisably because that's the name of the method we're about to use.

    So i'm going to take the json module i'm going to apply the dump method if you're wondering what dumps is read on or watch on you we'll find out later in this tutorial.

    And first i'm going to say what i'm dumping which is the list of characters now you can dump anything you can dump in fact it's a tuple of characters you can dump tuples you can dump lists you.

    Can dump dictionaries you can dump dictionaries of lists of tuples provided it's expressible in python you can store it in json format you'll just get an awful lot of indented brackets in.

    Your json file so the next thing i'm going to specify is where i'm dumping it to so i'm going to put the file path in and i'll also say how many indents i.

    Want in my file but you can see there's loads and loads of other ways you can configure writing to a json file to fine-tune what you get so i'll say with a file path i presume that stands for is.

    The name of my file which is json file and i think i'll have four indents that's it that will create my json file so if i now try running that program.

    You'll see it works and it's created a json file over there now if you eagle-eyed you'd actually see that this file already existed so just in case you think i'm.

    Fixing this let's delete that and run the program again and it will recreate the file so there's no fix it really is creating it and what you can see is it's taken this.

    And stored it was mostly with square brackets and this is what i talked about with serialization and what we're now going to do is look at how you can read that back in or deserialize it back into.

    The objects in python so having created a file called sortinghat.json with the previous part of the tutorial what we're now going to do is read that back in to whatever.

    Object is constructed so to do this i can firstly read in the file so i can say with open and i can specify my file name i just.

    Need some quotes around that and i don't need to specify the r because that's the default but i will do anyway and i'll call that json file.

    That will read it in and to load up a json file you literally i suppose i need a comment for this load json data into variable i literally take the name of a variable.

    Let's call it um potter people set that equal to json now i haven't imported the json module so i need to do that now before i.

    Do anything else and then i will be able to say json.load and then i can specify the file i'm loading it from which is json file and again there's lots of other.

    Information i can specify but i don't need to because i wrote it out in a fairly standard way so we'll read it back in a fairly standard way too so to see if that's worked.

    I can now print out the potter people variable and what you should see is you haven't quite got back what you wrote out so when we wrote this out.

    We had a tuple of a tuple and what we've got back is a list of a list so slightly change the data structure for us but that's okay.

    And if i wanted to be uh more specific i could for every single item i brought back i could print out the first and last name of the harry potter character so i could.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=wOlOD8vZjSI
Previous Post Next Post