1
00:00:00,140 --> 00:00:04,430
So let's go back to PowerShell. We'll first load XML data,

2
00:00:04,430 --> 00:00:08,020
then we'll look at how we load JSON data, and then we'll work

3
00:00:08,020 --> 00:00:11,070
on iterating XML and JSON data.

4
00:00:11,070 --> 00:00:13,470
So we looked previously at using variables,

5
00:00:13,470 --> 00:00:17,270
hash tables, arrays of information, but in reality,

6
00:00:17,270 --> 00:00:20,490
a lot of the data that we'll use will actually be from physical files.

7
00:00:20,490 --> 00:00:23,540
So I've just navigated to my data directory and you'll see I have a

8
00:00:23,540 --> 00:00:27,600
JSON file and some XML files that are available,

9
00:00:27,600 --> 00:00:33,640
So we're going to use this as a way of identifying data and retrieving it.

10
00:00:33,640 --> 00:00:35,460
So the first thing I want to do is obviously just

11
00:00:35,460 --> 00:00:44,200
specify my path Users\Trainer\Data,

12
00:00:44,200 --> 00:00:47,340
and that's going to be the path where the files are that we need to reside.

13
00:00:47,340 --> 00:00:50,690
Now if I want to retrieve and look at the XML data, I'm

14
00:00:50,690 --> 00:00:52,430
going to declare a variable here.

15
00:00:52,430 --> 00:00:59,090
I can say xml = and I can just use a standard Get‑Content where I pass

16
00:00:59,090 --> 00:01:07,250
a path like so and then I know my file is called module‑4.xml and I

17
00:01:07,250 --> 00:01:10,320
can just press Enter and then $xml, done,

18
00:01:10,320 --> 00:01:13,370
and I can see the information. So this gives me the countries

19
00:01:13,370 --> 00:01:16,150
with the names and the populations, etc. as we've been using

20
00:01:16,150 --> 00:01:18,770
previously, so fairly straightforward.

21
00:01:18,770 --> 00:01:22,290
Now what about if I needed to get the countries,

22
00:01:22,290 --> 00:01:28,420
for example, am I able to do this, for example. Nope.

23
00:01:28,420 --> 00:01:31,800
I have no way of accessing the various properties.

24
00:01:31,800 --> 00:01:33,570
I can retrieve the whole thing,

25
00:01:33,570 --> 00:01:39,540
but not individual values, and that's based on how we've selected the value.

26
00:01:39,540 --> 00:01:43,830
So what we can do instead is we have the path, what we can

27
00:01:43,830 --> 00:01:47,630
also do is declare what's called an xpath.

28
00:01:47,630 --> 00:01:52,160
So an xpath is effectively the structure that we wish

29
00:01:52,160 --> 00:01:55,360
to use from the XML definition.

30
00:01:55,360 --> 00:01:56,740
So, for example,

31
00:01:56,740 --> 00:01:59,170
if we, I tell you what if we just get rid of the xpath

32
00:01:59,170 --> 00:02:01,240
and just go back to XML for a second,

33
00:02:01,240 --> 00:02:05,920
you can see the xpath would be Countries Country, that

34
00:02:05,920 --> 00:02:08,830
would be the xpath that we want to go to,

35
00:02:08,830 --> 00:02:12,940
and then everything else underneath it would then come out of that.

36
00:02:12,940 --> 00:02:14,240
So if we go back here,

37
00:02:14,240 --> 00:02:19,610
we'll do $xpath = and then the way we use this is by doing

38
00:02:19,610 --> 00:02:25,090
slash, so I'm going to say Countries/Country like so. So I

39
00:02:25,090 --> 00:02:29,540
now have a path and an xpath.

40
00:02:29,540 --> 00:02:36,240
Now Microsoft provides a method called Select‑Xml.

41
00:02:36,240 --> 00:02:37,800
If I say Path,

42
00:02:37,800 --> 00:02:46,610
this is then going to be the path variable that we've got \module‑4.xml like

43
00:02:46,610 --> 00:02:56,040
so, and then we use the XPath option and I can pass my xpath to it, and then

44
00:02:56,040 --> 00:02:59,140
we can leave it as that and press Enter. Now,

45
00:02:59,140 --> 00:03:00,740
notice what happens.

46
00:03:00,740 --> 00:03:07,100
We get a really interesting thing come back. We get this node is country,

47
00:03:07,100 --> 00:03:09,640
then we get the path, and then we get a pattern, but we don't

48
00:03:09,640 --> 00:03:12,260
actually get the values back that we're expecting.

49
00:03:12,260 --> 00:03:16,540
So how do we change that? Well when we're using this type of data, we

50
00:03:16,540 --> 00:03:19,560
need to actually tell it what we'd like to get back.

51
00:03:19,560 --> 00:03:24,410
So Select‑Obj, and then we have a value called ExpandProperty and the

52
00:03:24,410 --> 00:03:27,970
one I wish to expand is the Node because remember,

53
00:03:27,970 --> 00:03:31,300
our XPath is countries and country and the nodes

54
00:03:31,300 --> 00:03:33,540
underneath it are the ones that we want.

55
00:03:33,540 --> 00:03:34,650
When we do that,

56
00:03:34,650 --> 00:03:38,510
it instantly returns the value back to us so now we have

57
00:03:38,510 --> 00:03:42,640
a list of values that come from XML.

58
00:03:42,640 --> 00:03:43,280
Now, of course,

59
00:03:43,280 --> 00:03:47,600
we can go a little bit further, there are ways of bringing other information in.

60
00:03:47,600 --> 00:03:50,830
So if we have other properties and values,

61
00:03:50,830 --> 00:03:53,840
then we can bring those values in and use them, so,

62
00:03:53,840 --> 00:03:54,350
for example,

63
00:03:54,350 --> 00:04:00,710
using attributes or other mechanisms. Now, XML has been around for a

64
00:04:00,710 --> 00:04:04,350
long time, it's not always the most up‑to‑date, one that people tend

65
00:04:04,350 --> 00:04:07,840
to use now, they like to use JSON instead.

66
00:04:07,840 --> 00:04:19,180
So let's update our path here to be the same, so Users\Trainer\Data like

67
00:04:19,180 --> 00:04:24,440
so, and what we'll do is create a JSON variable instead,

68
00:04:24,440 --> 00:04:38,320
and we'll do the same thing as before, so Get‑Content ‑Path $path\Module‑4.json,

69
00:04:38,320 --> 00:04:41,140
which is the file that we've got, and then we'll just press

70
00:04:41,140 --> 00:04:45,740
Enter and do $json, and of course we do.

71
00:04:45,740 --> 00:04:49,910
We get the JSON object listed, you can see it's got Countries, Country, Name,

72
00:04:49,910 --> 00:04:55,440
Population Validated, etc, the whole list is then provided.

73
00:04:55,440 --> 00:05:01,290
Now what PowerShell does for us is that we also have the option to

74
00:05:01,290 --> 00:05:09,440
take that same command and then say ConvertFrom Json.

75
00:05:09,440 --> 00:05:11,080
Now what difference did that make?

76
00:05:11,080 --> 00:05:11,480
Well,

77
00:05:11,480 --> 00:05:16,830
let's go back to json here and do a dot and you can see, if we go

78
00:05:16,830 --> 00:05:22,140
back, you just missed it there, Countries.Country.

79
00:05:22,140 --> 00:05:25,990
I can then start to iterate through the various

80
00:05:25,990 --> 00:05:29,650
properties that are available Now, some of them are closed,

81
00:05:29,650 --> 00:05:32,400
so I would have to use expanded properties, but when we

82
00:05:32,400 --> 00:05:36,770
use ConvertFrom‑Json, instead of it being just a static

83
00:05:36,770 --> 00:05:38,800
piece of text that comes back,

84
00:05:38,800 --> 00:05:43,920
it actually comes back as an actual JSON object so that

85
00:05:43,920 --> 00:05:46,240
we can then start to utilize that.

86
00:05:46,240 --> 00:05:49,530
Now, once we have the data stored like there,

87
00:05:49,530 --> 00:05:51,800
then we can do little things like we've done before,

88
00:05:51,800 --> 00:05:57,270
so I can say item in $json like so.

89
00:05:57,270 --> 00:06:00,130
So I'm going to say iterate through all of the JSON objects.

90
00:06:00,130 --> 00:06:13,930
I'm going to say item.Countries.Country, select Name and Population

91
00:06:13,930 --> 00:06:17,740
like so and then close that out and do Enter.

92
00:06:17,740 --> 00:06:23,940
Now, or you'll see I've got a extra one in there so let me go foreach loop,

93
00:06:23,940 --> 00:06:26,810
go back to there, it should have been a pipe command,

94
00:06:26,810 --> 00:06:30,490
not another bracket, and Enter.

95
00:06:30,490 --> 00:06:34,160
And so now because we have it as a JSON object,

96
00:06:34,160 --> 00:06:38,140
not a raw set of information that's come back, I can now

97
00:06:38,140 --> 00:06:40,220
start to use that as part of the queries.

98
00:06:40,220 --> 00:06:43,050
So that was a simple foreach statement where we're

99
00:06:43,050 --> 00:06:47,110
effectively saying for each object in the JSON,

100
00:06:47,110 --> 00:06:51,600
filter it out all the way down to country and then select the values.

101
00:06:51,600 --> 00:06:55,260
Now, of course, what you will see, let me just iterate through the fields again,

102
00:06:55,260 --> 00:07:00,680
is if I was to change this one, so let's do the for each, and if I

103
00:07:00,680 --> 00:07:06,850
got rid of country here and then did that, notice you'll get

104
00:07:06,850 --> 00:07:11,190
nothing back and that's because we haven't gone down to the lowest

105
00:07:11,190 --> 00:07:13,840
layer to be able to retrieve.

106
00:07:13,840 --> 00:07:25,050
That would need us to actually come into Select‑Object, Expand the property

107
00:07:25,050 --> 00:07:33,040
Country, then we would have to pipe that out to Select‑Object,

108
00:07:33,040 --> 00:07:35,740
and then we should be able to run.

109
00:07:35,740 --> 00:07:40,240
So depending on how far up the tree or how far you've gone up and down will

110
00:07:40,240 --> 00:07:44,240
determine whether you need to expand the various properties.

111
00:07:44,240 --> 00:07:45,280
Okay,

112
00:07:45,280 --> 00:07:48,840
so one other last thing that I'd like to talk about is really

113
00:07:48,840 --> 00:07:55,950
to do with Json, but in the real world, when you're accessing things like JSON,

114
00:07:55,950 --> 00:08:02,070
you're not necessarily calling it from a static file that someone has given you.

115
00:08:02,070 --> 00:08:08,530
So what I'm going to do here is there is a series of endpoints that

116
00:08:08,530 --> 00:08:10,940
are available for anybody to use for testing.

117
00:08:10,940 --> 00:08:13,640
So I'm going to use one of these and this is called, it's at

118
00:08:13,640 --> 00:08:16,840
swapi.dev and it's called api and people.

119
00:08:16,840 --> 00:08:22,780
Now, in order to populate the JSON from an external location,

120
00:08:22,780 --> 00:08:27,500
I'm going to use Invoke‑RestMethod and my Uri is

121
00:08:27,500 --> 00:08:33,140
going to be this uri that I created, which basically goes to API people.

122
00:08:33,140 --> 00:08:37,130
So I'm going to enter that there and then $json. If I look at it,

123
00:08:37,130 --> 00:08:40,130
you can see it's come back with some results.

124
00:08:40,130 --> 00:08:40,560
You can also,

125
00:08:40,560 --> 00:08:43,060
you can see Skywalker already so you get an idea of

126
00:08:43,060 --> 00:08:44,940
what the data is that's coming back.

127
00:08:44,940 --> 00:08:50,580
So what I can then do is do a foreach and say $item or you could

128
00:08:50,580 --> 00:08:53,200
give it a different name so like people. Now,

129
00:08:53,200 --> 00:08:56,350
when you're calling JSON from an invoke method,

130
00:08:56,350 --> 00:09:00,890
you'll always get a results object, that's normally how they're

131
00:09:00,890 --> 00:09:04,930
written so you can just call into that value.

132
00:09:04,930 --> 00:09:10,250
I can then say item and then I can just select the values

133
00:09:10,250 --> 00:09:13,940
that I want to get back, so Name, Height,

134
00:09:13,940 --> 00:09:17,840
Gender, for example, and then close that out.

135
00:09:17,840 --> 00:09:18,630
And sure enough,

136
00:09:18,630 --> 00:09:22,390
you can see that I now get a list of Star Wars characters with

137
00:09:22,390 --> 00:09:26,560
their height and their gender from an external API endpoint that

138
00:09:26,560 --> 00:09:30,990
was available and returning JSON. So not too complicated, but

139
00:09:30,990 --> 00:09:32,510
it's a real case scenario.

140
00:09:32,510 --> 00:09:35,160
There were lots of times where I've written PowerShell scripts that

141
00:09:35,160 --> 00:09:39,840
rely on an endpoint to either return XML or JSON,

142
00:09:39,840 --> 00:09:43,440
and so it's important to understand how to get those values back.

143
00:09:43,440 --> 00:09:44,340
In the worst case,

144
00:09:44,340 --> 00:09:48,720
you would always use convert from JSON on the object, so for example,

145
00:09:48,720 --> 00:09:51,570
if I say item here and then do the name,

146
00:09:51,570 --> 00:09:56,690
you can see that because it comes back as a results object, I get all of

147
00:09:56,690 --> 00:10:01,340
the values that are available to me. So you'll see one of those was films

148
00:10:01,340 --> 00:10:05,250
and it comes back with links to films to other entry points that are

149
00:10:05,250 --> 00:10:08,060
available, but I could say birth year, etc.

150
00:10:08,060 --> 00:10:12,320
So when you bring in data back from static places,

151
00:10:12,320 --> 00:10:14,740
whether it's from a database or something else,

152
00:10:14,740 --> 00:10:22,000
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.

