Convert to/from JSON in Powershell!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so let's look at some new commandlets in Powershell 3 convert to and convert from Json what is Json this is a good way to think of Json what is a CSV file a CSV file is just a way to represent data in the format of a table and lots of programs and programming languages use tables to represent data it's a very common way of representing data so CSV files are just a simple format that many programs can use to exchange data with each other because they understand that data but maybe they can't directly communicate with each other uh in the same way that um when I was traveling through Europe I noticed everybody talked to each other in English but they all had their own languages from their specific countries so English was just a way for them to easily exchange information with each other because they you know you can't interface uh German with Italian it's just not going to work you can go through all the effort of trying to interpret it in in the same way that you could um download a website and try to interpret the data but it's easier to just have a common format uh that everybody understands so in the world of programming now everything is sort of objectoriented so there needs to be a way to efficiently communicate objects between different different entities and CSV files aren't really good for that so one way of doing this is Json which stands for JavaScript object notation and it's basically a methodology for um interpreting object data so we have get process here and we are going to um take get process and we're going to straight up convert it to Json and then add it to a file and we can just look directly at some Json uh it's that simple so I've just created a Json file from a get process object so you can see this is a little bit more complicated than a CSV file and it's got lots of stuff lots of data uh Json supports uh data types and arrays and all sorts of things um so here's all the data and I just wanted to quickly show you uh that Json it's you know it's got its own formatting its own own stuff so you can look that up if you want to know more about Json um but we're going to focus on the commandlets so that is how you can take an object from within Powershell and convert it to Json and then you can send it off to somebody who might need a Json format uh but what if you want to import Json well we have convert from Json and this adds a lot of of opportunities uh within Powershell and I'm about to show you one of the really cool ways um that we can utilize Json so there's a website called redit and uh it's basically a website about cats and they provide a Json API to interface with this website because it's it's essentially um a a picture sharing comments based website it's really simple stuff it consists of of text and uh pictures so they can represent that stuff as a Json object file so let's look at what that looks like we can go directly to their website and this is this is reddit.com in Json so you saw the front page all I had to do is add slash. Json and it shows me the website in Json so we can convert this in Powershell so how do we get this to Powershell well we just use some other some other little tricks uh we're using a net um object here called Web client which will allow us to download a URI as a string so in this case we're pro providing it the Reddit URI for their Json and then we're pipelining it to convert from J Json so that's really simple then we have this object called Json so since this is it started out as a string but once we use convert from Json it turns this into a rich data object and we can look at different properties within this object so we can see uh I guess they've decided to tell you what kind of the uh object this is so it's a listing so this is actually um reddits configuration they they uh created this object model so um what does their API or what does their Json API provide it essentially provides the raw um values that you see on the front page so we can search through all of these different values from that Json so let's go ahead and take a look at that uh what we need to do is first dig down inside of where uh they have structured it so in data children um item zero we can see there's another data so this is the first post on reddit.com I believe so we can look for the title where's the title title okay my life so let's see you can see the first one is my life and we have 93 comments uh we have their the link it sends you to it's subreddit uh when it was submitted all the likes so all that information can now be seen here and that's really all you need if you want to see the URL if you want to see the title to know what it's about um the peral link takes you to all the uh comments so you can get a synopsis of what's going on on the Reddit front page just by searching through this data um and that's all I wanted to show you so let's well I'll just show you real quick the keys um I've organized everything so that uh if you run Keys it just displays everything on the front page so that's it that's all I wanted to show you is the new um convert 2 and convert from Json commandlets in Powershell 3 um I I think it it adds a a lot of really cool abilities to Powershell um and there was some uh modules or extensions I think you could use before to add this functionality but now they're built directly into Powershell and you can do really cool stuff like uh browse Reddit at work without your boss knowing you could you know be like yeah I'm KN deep and code I'm just trying to you know figure out where this string is over here and search stuff so that's it thanks for watching
Info
Channel: MrPowerScripts
Views: 14,523
Rating: undefined out of 5
Keywords: ConvertFrom-Json, json powershell, powershell convert json, json tutorial powershell, json powershell tutorial, powershell json to object, json script powershell tutorial, json conversion powershell, json conversion script powershell, json script powershell, Converto-Json, automate json, json scripting tutorial, JSON, json scripting, mrpowerscripts
Id: Mrq9MBwivn8
Channel Id: undefined
Length: 7min 40sec (460 seconds)
Published: Thu Apr 19 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.