So let's go back to PowerShell. We'll first load XML data,
then we'll look at how we load JSON data, and then we'll work
on iterating XML and JSON data.
So we looked previously at using variables,
hash tables, arrays of information, but in reality,
a lot of the data that we'll use will actually be from physical files.
So I've just navigated to my data directory and you'll see I have a
JSON file and some XML files that are available,
So we're going to use this as a way of identifying data and retrieving it.
So the first thing I want to do is obviously just
specify my path Users\Trainer\Data,
and that's going to be the path where the files are that we need to reside.
Now if I want to retrieve and look at the XML data, I'm
going to declare a variable here.
I can say xml = and I can just use a standard Get‑Content where I pass
a path like so and then I know my file is called module‑4.xml and I
can just press Enter and then $xml, done,
and I can see the information. So this gives me the countries
with the names and the populations, etc. as we've been using
previously, so fairly straightforward.
Now what about if I needed to get the countries,
for example, am I able to do this, for example. Nope.
I have no way of accessing the various properties.
I can retrieve the whole thing,
but not individual values, and that's based on how we've selected the value.
So what we can do instead is we have the path, what we can
also do is declare what's called an xpath.
So an xpath is effectively the structure that we wish
to use from the XML definition.
So, for example,
if we, I tell you what if we just get rid of the xpath
and just go back to XML for a second,
you can see the xpath would be Countries Country, that
would be the xpath that we want to go to,
and then everything else underneath it would then come out of that.
So if we go back here,
we'll do $xpath = and then the way we use this is by doing
slash, so I'm going to say Countries/Country like so. So I
now have a path and an xpath.
Now Microsoft provides a method called Select‑Xml.
If I say Path,
this is then going to be the path variable that we've got \module‑4.xml like
so, and then we use the XPath option and I can pass my xpath to it, and then
we can leave it as that and press Enter. Now,
notice what happens.
We get a really interesting thing come back. We get this node is country,
then we get the path, and then we get a pattern, but we don't
actually get the values back that we're expecting.
So how do we change that? Well when we're using this type of data, we
need to actually tell it what we'd like to get back.
So Select‑Obj, and then we have a value called ExpandProperty and the
one I wish to expand is the Node because remember,
our XPath is countries and country and the nodes
underneath it are the ones that we want.
When we do that,
it instantly returns the value back to us so now we have
a list of values that come from XML.
Now, of course,
we can go a little bit further, there are ways of bringing other information in.
So if we have other properties and values,
then we can bring those values in and use them, so,
for example,
using attributes or other mechanisms. Now, XML has been around for a
long time, it's not always the most up‑to‑date, one that people tend
to use now, they like to use JSON instead.
So let's update our path here to be the same, so Users\Trainer\Data like
so, and what we'll do is create a JSON variable instead,
and we'll do the same thing as before, so Get‑Content ‑Path $path\Module‑4.json,
which is the file that we've got, and then we'll just press
Enter and do $json, and of course we do.
We get the JSON object listed, you can see it's got Countries, Country, Name,
Population Validated, etc, the whole list is then provided.
Now what PowerShell does for us is that we also have the option to
take that same command and then say ConvertFrom Json.
Now what difference did that make?
Well,
let's go back to json here and do a dot and you can see, if we go
back, you just missed it there, Countries.Country.
I can then start to iterate through the various
properties that are available Now, some of them are closed,
so I would have to use expanded properties, but when we
use ConvertFrom‑Json, instead of it being just a static
piece of text that comes back,
it actually comes back as an actual JSON object so that
we can then start to utilize that.
Now, once we have the data stored like there,
then we can do little things like we've done before,
so I can say item in $json like so.
So I'm going to say iterate through all of the JSON objects.
I'm going to say item.Countries.Country, select Name and Population
like so and then close that out and do Enter.
Now, or you'll see I've got a extra one in there so let me go foreach loop,
go back to there, it should have been a pipe command,
not another bracket, and Enter.
And so now because we have it as a JSON object,
not a raw set of information that's come back, I can now
start to use that as part of the queries.
So that was a simple foreach statement where we're
effectively saying for each object in the JSON,
filter it out all the way down to country and then select the values.
Now, of course, what you will see, let me just iterate through the fields again,
is if I was to change this one, so let's do the for each, and if I
got rid of country here and then did that, notice you'll get
nothing back and that's because we haven't gone down to the lowest
layer to be able to retrieve.
That would need us to actually come into Select‑Object, Expand the property
Country, then we would have to pipe that out to Select‑Object,
and then we should be able to run.
So depending on how far up the tree or how far you've gone up and down will
determine whether you need to expand the various properties.
Okay,
so one other last thing that I'd like to talk about is really
to do with Json, but in the real world, when you're accessing things like JSON,
you're not necessarily calling it from a static file that someone has given you.
So what I'm going to do here is there is a series of endpoints that
are available for anybody to use for testing.
So I'm going to use one of these and this is called, it's at
swapi.dev and it's called api and people.
Now, in order to populate the JSON from an external location,
I'm going to use Invoke‑RestMethod and my Uri is
going to be this uri that I created, which basically goes to API people.
So I'm going to enter that there and then $json. If I look at it,
you can see it's come back with some results.
You can also,
you can see Skywalker already so you get an idea of
what the data is that's coming back.
So what I can then do is do a foreach and say $item or you could
give it a different name so like people. Now,
when you're calling JSON from an invoke method,
you'll always get a results object, that's normally how they're
written so you can just call into that value.
I can then say item and then I can just select the values
that I want to get back, so Name, Height,
Gender, for example, and then close that out.
And sure enough,
you can see that I now get a list of Star Wars characters with
their height and their gender from an external API endpoint that
was available and returning JSON. So not too complicated, but
it's a real case scenario.
There were lots of times where I've written PowerShell scripts that
rely on an endpoint to either return XML or JSON,
and so it's important to understand how to get those values back.
In the worst case,
you would always use convert from JSON on the object, so for example,
if I say item here and then do the name,
you can see that because it comes back as a results object, I get all of
the values that are available to me. So you'll see one of those was films
and it comes back with links to films to other entry points that are
available, but I could say birth year, etc.
So when you bring in data back from static places,
whether it's from a database or something else,
then convert from JSON so it can become an object that's usable. If you're calling from an API, it will probably do that for you.