PowerShell 7 Tutorials for Intermediates #7 : Working with JSON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to the seventh video in our Powershell 7.2 for intermediate tutorial Series in the last video we saw how to manipulate and Import and Export some CSV files in this video we're going to be taking a look at Json files and Json formatting in general Json is very useful to use in Powershell especially once you start getting into using apis either calling apis um for the get method or even for the put or post methods oftentimes you'll have to pass in a body which will have to be formatted in a Json format so we will see all of that uh today and let's go ahead and let's actually get started here uh so what I actually have let me just delete this file because we're going to be creating this file so what I actually have here is I have a Json file already created it's actually a it's got a parent so it's got some depth to it so the first level is employees and then that contains an array of our employees so that is our generic Json file that we'll be working with today um and then I also have a CSV file that we're going to show you how to convert this CSV file into a Json file so let's actually go ahead and let's get started here so the first thing we're going to want to do like always when we work with files is we're going to want to specify the file path to our employees.json file so let's go ahead and let's create our variable here called Json file path and we are going to make that equal to our file path so let's just copy that over here and then what we're going to do is let's take a look and see what it looks like so let's do a get content on the path of the Json file path the ones we actually go ahead and look at this doesn't seem too too useful we just get exactly what our file was so we have the open curly brackets our employees and then the start of the array and then we have all of our objects in there now what we would do in Powershell is actually then just pipe this get content to the convert from Dash Json and if we run this here we are going to see that we actually get our employees and then we have all of our employee objects so let me just sort store that here so let's create another variable called Json data and we're going to make that equal to the get content of our Json file path and then we pipe that to convert from Json and then what we could do here is do a Json data dot employees and if we run all of these lines here there is our files so we actually have all of our employees now we would be able to Loop through all of these objects here so what we could actually do is once again just create a new variable called employee data and then in our employee data now are going to be all of our employees which we would then be able to Loop through and either use them in our active directory script if we want to automate active directory or if you wanted to search as well what we could do is do employee data and we could pipe that where um let's see where where Dash object and that we can say title is equal to programmer so let's pull back all of our programmers here and there is our programmer in our employee data so if we actually look at that we will see that we have a junior engineer a senior engineer and a CEO and one programmer so that worked perfectly fine uh we can even manipulate um the dates as well we could look up the employee ID so it's very very easy to use Json very similarly to a CSV file if you have all of your data in there so that is really the basics on how to work with Json files now let's go ahead and let's take a look at how we can convert our employees CSV to a Json formatted um document here so what we're going to want to do is we're just going to want to go ahead and create another variable here called CSV file path and we're just going to make that equal to our CSV path here and then what we're going to want to do is create a CSV data variable and we could do an import Dash CSV and then our path is going to be CSV file path now if we run that here we can then look at the CSV data we can see that we have everything there we have our ID we have our name title country in this one and all we need to do here is do a CSV data and we can actually pipe that then to convert to Json and if we look at this here we will actually see that we have a nicely formatted Json just without that depth that does say employees but we you don't need that and it's actually probably easier if you don't have that if you're just trying to work with employee data if you already know that everything in that file is just employees uh it is a hundred percent okay and then what you could do is you can then pipe that to an out Dash file and then you would just at that point give it a file path here so let me do a c grips intermediate tutorials Json files and then export to Json dot Json if we go ahead and we run that here we will see our Json file is now here all created with that CSV employee data so if you were trying to migrate all your data from CSV files to Json files that's how you would do that now comes the probably the most useful part of working with Json files and this is probably how you're going to be manipulating Json most of the time through Powershell you might have Json files for like configuration files configuration files for me often are XML files or Json files but you're always going to be working with Json if you're working with apis uh now we've already seen a video where we've compared invoke web request compared to invoke rest method we're going to be using invoke web request in this one to really kind of manipulate that Json so we're going to be using a free API today so we're going to create a variable called URI and this is going to be our API URL so it's actually just https colon backslash backslash rest countries dot com and then slash V2 slash all and then what we're going to do is we are then going to create a variable called Web request which is going to be equal to invoke Dash web request we're going to specify our URI here and we're also going to specify our method which is going to be get now if we actually just run these two lines here and we look at our web request we can see that we do get a status code of 200 which is perfect a status description of ok it means it worked fine and then we have our content now our content is where our Json is going to be from our API call so if we actually look at webrequest dot content here we're going to see there's a lot of stuff what this API does it actually gets us all the country's information so it'll give us their top level domain name it'll give us their country name it'll give us their Capital uh the different translations for the countries but as you can see it looks very very ugly when we just look at content now this is where you're going to want to use that pipe and then use a convert from Dash Json and if we look at this here as you can already tell it's already getting cleaned up we have all of these objects coming back so what we can actually do here is just do a dollar sign a country data equals the web request content convert from Json and then what we could actually do is do a country data and we can actually pipe that to where Dash object name is equal and we can see where it's equal to Canada here and here we have all of our Canada information from this API so we have our Canada we have our top level domain.ca there's the different types of country codes via two character free character the calling code the capital we have the sub region and the regions or the continent that it is on uh we have the latitude and longitude we have the population of 38 million um you are a Canadian we also have the different types of currencies here as well and the different types of languages that are spoken in the country the translation and there's even a link to the flag and that would be a just a image of the flag of Canada but we can also do if where name where United States and if we look at this info here it is not there I believe it might be USA Maybe um so here's another great tip so when you cannot find a country what I would typically do is a dash like and then United star and if we run that here we have United oh so the name is United States of America uh so if we did United States uh America here and we ran this we would then see all the information for United States we would see the top level domain is dot us and then we could see the population but as you can see there are a lot of great information that you can actually use by converting it from Json you're you're just turning this very useless data that you can't really parse in Powershell you are then creating a bunch of Powershell objects from that and then you can use the Powershell where object sorts um select object to get that information that you really want now this is simply getting information from an API where we're getting back Json there are times where you're going to have to specify a body um for a post or a put sometimes even a get will have a body depending on the type of API we're going to be working a lot with apis we're going to be creating a lot of bodies when we're working with elastic in a few weeks um so I'm just going over some of the basics here this way you guys can get familiar with it so typically what we would do if we have a put or a post and we need to pass in a body what there are two ways that we could do it and I'm going to go over the two ways the first way is simply creating our Json text directly in Powershell the other way is creating a Powershell custom object and then converting that to Json both ways are very very useful it really just depends on what you're more comfortable with doing so let's actually go ahead and let's just see how we would write Json directly in Powershell here so let's do a Json body variable and we're going to make that equal to an at symbol and then double quotes we're going to be creating a literal string this is very important when you're creating Json objects it does make it a lot easier to kind of see I find it easier to create Json objects when I could see the formatting with the curly brackets so we're going to start our Json object with just some curly brackets here and we're just going to create a very simple employee object we're going to do employee ID and colon and we're going to put our employee ID as a thousand and five then we're going to do a comma here and we're just going to put a name and we are going to put a colon and we're going to make that name equal to jacked programmer and that's all we're going to do that's going to be our Json body this is what we're passing into our API let's say so now if we look at Json body afterwards we will see it looks just like that which is perfectly fine now if what we did is we did a convert from Json just to see what it would look like as a Powershell object there it is we have our employee ID we have our name so this could be a very easy way to create Powershell objects is to create a Json and then just convert from Json and you'll have your Powershell object if you're wanting to manipulate objects in Powershell the other option is to create a Powershell object and then convert that to Json if you wanted to use that in an API call now let's go ahead let's create our test object here and we're going to make that equal to new object and we are going to do a type name PS custom object and let's go ahead and let's do a add member here on the input object of test object the member type is going to be a note property the name of our property is going to be employee ID and the value here is going to be 1006 let's say and we're just going to add another line here we're going to call it name and let's put that here to test all right so once we have our object here we're just going to look at it once more after we create it just so you guys can see what our object looks like here it is we have employee ID we have name all we would need to do now is then convert from oh convert to Json and if we run that here then we would have our Json and then all you would need to do is make that equal to a body and then you would be able to then pass in this body through our invoke rust method or invoke web request oftentimes if you're using a put or post you will see that you can specify a body and that body is usually needs to be formatted in Json of course the API documentation will confirm that but I haven't really seen anything that's not Json for apis but I could be wrong on that but most of the time you will be fully manipulating it with Json and we're going to be seeing a lot of this in our next videos with elastic so we only have one more file type to look at and manipulate with Powershell and that is going to be the XML file type so be sure to stay tuned for that if you guys have any comments or questions list Json files please let me know Down Below in the comments I will be glad to answer your questions or even make another video that clarifies something or goes more in depth in a topic that you guys want as well be sure to hit that subscribe button hit that like button hit that notification Bell to be notified when that next video comes out and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 2,541
Rating: undefined out of 5
Keywords: powershell 7, windows powershell, windows 10, visual studio code powershell, programming, coding, scripting, powershell, powershell for beginners, how to start with powershell, scripts, powershell 7.2, powershell 7 windows 11, windows 11, how to, methods in powershell, powershell scripting, functions in powershell, cmdlets in powershell, powershell tutorial, csv, powershell csv, powershell json, json, json tutorial, json tutorial powershell, json data, api, powershell api
Id: ycr6RUjCl9c
Channel Id: undefined
Length: 17min 33sec (1053 seconds)
Published: Thu Dec 01 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.