WEBVTT 0:00:02.980000 --> 0:00:08.080000 Hello and welcome this video titled encoding data with JSON. 0:00:08.080000 --> 0:00:16.640000 So, in a previous video, we did a review of REST-based APIs. 0:00:16.640000 --> 0:00:21.780000 I said that some characteristics of a REST-based API is that it's a web 0:00:21.780000 --> 0:00:27.800000 -based API, so it uses IP with HTTP behind it to talk to devices, that 0:00:27.800000 --> 0:00:34.460000 within that HTTP it uses common HTTP verbs like get, push, post, delete, 0:00:34.460000 --> 0:00:38.280000 that we would normally find even in interactions with a common website, 0:00:38.280000 --> 0:00:39.740000 that REST uses that. 0:00:39.740000 --> 0:00:45.060000 And I mentioned toward the end that the structure of the data that REST 0:00:45.060000 --> 0:00:49.800000 uses to push data and retrieve data has to be a standard data structure. 0:00:49.800000 --> 0:00:53.620000 And I said that that's typically represented as either JSON or XML. 0:00:53.620000 --> 0:00:57.780000 So, let's take a quick look here at what JSON data actually looks like. 0:00:57.780000 --> 0:01:03.740000 So, notice it is pronounced JSON and it stands for JavaScript Object Notation. 0:01:03.740000 --> 0:01:09.400000 So, it is a subscript or a subset of JavaScript syntax. 0:01:09.400000 --> 0:01:12.300000 So, if you know anything about JavaScript, you're already ahead of the 0:01:12.300000 --> 0:01:14.100000 game by knowing about JSON. 0:01:14.100000 --> 0:01:22.140000 So, the actual data that's pushed down to a client like a router or switch 0:01:22.140000 --> 0:01:26.820000 or pulled up is called a JSON object. 0:01:26.820000 --> 0:01:28.580000 So, that's how we represent the data. 0:01:28.580000 --> 0:01:31.840000 That's how we represent the IP address, the interface name, the route 0:01:31.840000 --> 0:01:33.020000 from the routing table. 0:01:33.020000 --> 0:01:36.840000 These are all represented as JSON objects. 0:01:36.840000 --> 0:01:41.680000 So, what are some of the benefits of doing this? 0:01:41.680000 --> 0:01:47.840000 Well, it's lightweight, language independent, and easy to read and write. 0:01:47.840000 --> 0:01:53.500000 For example, if you see here, notice on the left, we have two pieces of 0:01:53.500000 --> 0:01:57.760000 data in both blocks here, in both blue blocks, we have the exact same 0:01:57.760000 --> 0:02:03.680000 piece of data, which is information about employees. 0:02:03.680000 --> 0:02:08.460000 And it looks like billing codes, shipping codes. 0:02:08.460000 --> 0:02:13.020000 Now, on the left, we took that exact, so it looks like it's account information. 0:02:13.020000 --> 0:02:17.180000 On the left, we took our account information and we are representing it 0:02:17.180000 --> 0:02:22.120000 in a standard format called XML, extensible markup language. 0:02:22.120000 --> 0:02:26.680000 And it works. You can read through that and you can make sense of it. 0:02:26.680000 --> 0:02:30.860000 We can see here that we have an account, the very first account. 0:02:30.860000 --> 0:02:34.160000 We have an account number beginning with 1242. 0:02:34.160000 --> 0:02:38.460000 We have, it looks like there's a space for a phone number, but that space 0:02:38.460000 --> 0:02:41.200000 is empty possibly. 0:02:41.200000 --> 0:02:43.780000 We have a website for that account. 0:02:43.780000 --> 0:02:46.300000 And so, you know, you can read through that. 0:02:46.300000 --> 0:02:50.720000 On the right, we have that exact same account information represented 0:02:50.720000 --> 0:02:54.580000 in a completely different data format called JSON. 0:02:54.580000 --> 0:02:57.180000 And honestly, if you're comparing the two boxes, I think you would agree 0:02:57.180000 --> 0:03:01.200000 with me that the box on the right, it's just easier to read it. 0:03:01.200000 --> 0:03:02.960000 It's more human readable, right? 0:03:02.960000 --> 0:03:05.740000 You can go through that and it's easier to parse. 0:03:05.740000 --> 0:03:08.240000 It certainly takes up far less character. 0:03:08.240000 --> 0:03:12.920000 So when talking about lightweight, JSON is far more lightweight than XML, 0:03:12.920000 --> 0:03:17.440000 because it's not nearly as many characters and special characters in order 0:03:17.440000 --> 0:03:19.340000 to represent the same data. 0:03:19.340000 --> 0:03:26.340000 So both XML and JSON are standard ways to format data and REST-based APIs 0:03:26.340000 --> 0:03:31.480000 can use either one of those, but usually they choose to use JSON because 0:03:31.480000 --> 0:03:39.040000 just as you can visually see here, JSON is just easier to work with. 0:03:39.040000 --> 0:03:51.320000 All right. So, JSON can use and represent different types of data. 0:03:51.320000 --> 0:03:55.800000 And this is just a high level over here of about three or four of about 0:03:55.800000 --> 0:03:59.720000 seven or eight different types of data that JSON can use. 0:03:59.720000 --> 0:04:01.940000 There's something called a JSON object. 0:04:01.940000 --> 0:04:10.000000 You'll always recognize an object because it is surrounded by a JSON object. 0:04:10.000000 --> 0:04:14.660000 So whenever you're reading through JSON and you see curly brackets, that 0:04:14.660000 --> 0:04:18.360000 is an object. So for example, if we go back to where we just were, right 0:04:18.360000 --> 0:04:24.480000 here, here's the curly bracket, and that ends right here. 0:04:24.480000 --> 0:04:31.660000 So all this stuff in here is a JSON object started by and ended by a curly 0:04:31.660000 --> 0:04:41.700000 bracket. Now within a JSON object, we have these things called name value 0:04:41.700000 --> 0:04:48.760000 pairs. For example, fast ethernet 00 IP address. 0:04:48.760000 --> 0:04:50.220000 That would be a name value pair. 0:04:50.220000 --> 0:04:51.720000 Here's the name fast ethernet. 0:04:51.720000 --> 0:04:52.480000 Here's the value. 0:04:52.480000 --> 0:04:57.160000 Here's its IP address or fast ethernet 00 up. 0:04:57.160000 --> 0:05:00.720000 Here's a name value pair, the name, and this case, the value is the state 0:05:00.720000 --> 0:05:09.040000 of that interface. 0:05:09.040000 --> 0:05:11.020000 So we're going to go back to that previous example. 0:05:11.020000 --> 0:05:13.180000 So we're going to go back to that previous example. 0:05:13.180000 --> 0:05:18.120000 So notice here, here is a name value pair. 0:05:18.120000 --> 0:05:23.220000 Number of employees is the name and the value of that is three. 0:05:23.220000 --> 0:05:28.280000 And because that's one of several name value pairs within this object, 0:05:28.280000 --> 0:05:33.040000 there's a comma, which indicates there's another name value pair coming 0:05:33.040000 --> 0:05:36.860000 right up. It's very important. 0:05:36.860000 --> 0:05:41.740000 Another thing is that know that between the name and the value, we have 0:05:41.740000 --> 0:05:52.420000 a colon. All right, so we're going to have name colon value and notice 0:05:52.420000 --> 0:05:57.860000 that both the name and the value are going to be enclosed in double quotes. 0:05:57.860000 --> 0:06:01.600000 And if there's another name value, there's going to be a comma, and then 0:06:01.600000 --> 0:06:03.480000 we're going to repeat it right here. 0:06:03.480000 --> 0:06:05.740000 This is the actual structure of JSON. 0:06:05.740000 --> 0:06:10.900000 JSON says you have to write it, you have to create it just like this. 0:06:10.900000 --> 0:06:18.800000 A string, this is another type of data. 0:06:18.800000 --> 0:06:20.380000 Must be enclosed in double quotes. 0:06:20.380000 --> 0:06:22.300000 So for example, there's a string. 0:06:22.300000 --> 0:06:26.280000 A string is basically letters. 0:06:26.280000 --> 0:06:30.740000 So non-numerical fields are strings. 0:06:30.740000 --> 0:06:34.520000 And then we have numbers, integers or floats. 0:06:34.520000 --> 0:06:40.700000 So for example, here our name is a string. 0:06:40.700000 --> 0:06:46.200000 Here our value is an integer. 0:06:46.200000 --> 0:06:53.740000 If the value had looked like this, with a decimal point, that would be 0:06:53.740000 --> 0:06:55.640000 considered a float. 0:06:55.640000 --> 0:06:59.840000 Still a number is just the presence of a decimal point now converts it 0:06:59.840000 --> 0:07:03.560000 into a different type of data called a float instead of an integer. 0:07:03.560000 --> 0:07:12.180000 Syntax rules. So data is a name value pairs. 0:07:12.180000 --> 0:07:15.500000 Multiple name value pairs are separated by commas. 0:07:15.500000 --> 0:07:22.560000 Curly braces hold objects, square brackets hold arrays. 0:07:22.560000 --> 0:07:26.820000 Raise. I think we'll see an example of an array coming up. 0:07:26.820000 --> 0:07:30.940000 Spaces and line breaks don't matter. 0:07:30.940000 --> 0:07:36.480000 So for example, that is valid JSON syntax right there. 0:07:36.480000 --> 0:07:40.780000 So notice we've got our curly brackets here and here. 0:07:40.780000 --> 0:07:47.260000 So everything in here is one object. 0:07:47.260000 --> 0:07:49.900000 Okay, and then we have a name value pair. 0:07:49.900000 --> 0:07:52.000000 So here are our first name value pair. 0:07:52.000000 --> 0:07:53.660000 So this is our first name. 0:07:53.660000 --> 0:07:57.220000 So we have a string called instructor. 0:07:57.220000 --> 0:07:59.620000 Okay, what's the value of that? 0:07:59.620000 --> 0:08:04.780000 Oh, look, the value is enclosed in square brackets. 0:08:04.780000 --> 0:08:13.420000 So this whole thing in square brackets is called an array. 0:08:13.420000 --> 0:08:16.740000 And our array is consisting of further name value pairs. 0:08:16.740000 --> 0:08:18.580000 Here's a name value pair in our array. 0:08:18.580000 --> 0:08:20.500000 Here's another name value pair. 0:08:20.500000 --> 0:08:24.480000 And notice we're using commas in between them. 0:08:24.480000 --> 0:08:27.220000 And it says spaces and line breaks don't matter. 0:08:27.220000 --> 0:08:32.880000 So that is valid or you could represent it like that. 0:08:32.880000 --> 0:08:34.320000 That also works. 0:08:34.320000 --> 0:08:41.360000 That's probably actually the more common way of representing JSON format. 0:08:41.360000 --> 0:08:46.100000 So that's all you really need to know for your introduction and refresher 0:08:46.100000 --> 0:08:49.060000 on JSON. I hope you found that helpful.