WEBVTT 00:01.930 --> 00:02.760 Welcome back. 00:03.070 --> 00:08.770 In this lecture, you're going to learn how to get your values back by decoding the JSON data that we 00:08.770 --> 00:10.480 have created in the last lecture. 00:13.970 --> 00:19.850 OK, in the previous lecture, you had saved the output of the encoding to the users JSON file. 00:20.360 --> 00:26.890 So now I'm going to read it into a bit less, then I'm going to decode it back into a user's slice. 00:27.710 --> 00:31.370 First, I'm going to declare a new bytes for the input. 00:32.030 --> 00:35.050 I'm going to use a scanner to read the jassam from the file. 00:35.630 --> 00:38.240 So I'm going to create a new scanner in a loop. 00:40.490 --> 00:42.440 And I'm going to scan for the lines. 00:43.480 --> 00:48.940 Inside the loop, I'm going to add the bites of the next line from the Jason file to the input slice. 00:50.450 --> 00:52.940 Let me show you what the input looks like. 00:54.060 --> 00:57.960 I'm going to run the program by feeding it JSON file LACHSA. 00:58.230 --> 01:01.200 As you can see, the input contains adjacent data. 01:01.230 --> 01:03.940 Now the scanner removes the new lines. 01:03.990 --> 01:06.090 That's why the output looks like so. 01:06.630 --> 01:08.970 But it's not a problem for the adjacent package. 01:09.240 --> 01:10.230 It can read this. 01:10.500 --> 01:12.420 I can add a new line here, Lexar. 01:13.830 --> 01:15.220 Now it looks better, right? 01:15.600 --> 01:20.080 However, as I said, doing so is unnecessary, so I'm not going to add new lines. 01:20.910 --> 01:23.310 You got the jassam, now it's time to decode it. 01:24.180 --> 01:27.060 I'm going to decode the users into the user slice. 01:27.210 --> 01:28.760 So I'm going to declare it like, sup? 01:29.680 --> 01:34.120 Of course, you don't have the USA's track here, let me declare it LACHSA. 01:36.830 --> 01:42.740 E JSON object contains a user name field, so I'm going to declare the name field as a shrink. 01:44.050 --> 01:51.090 The Jason data doesn't contain the password fields, so I won't include them in the truck either eat 01:51.100 --> 01:57.230 JSON object has a purpose object and each Perm's object contains key value pairs. 01:58.150 --> 02:00.400 So I'm going to add a Matfield like so. 02:02.670 --> 02:07.620 OK, it is time to decode the Jason to the users slice to do that. 02:07.650 --> 02:09.870 I'm going to call the Marshall function. 02:10.380 --> 02:13.360 This function is the opposite of the Marshall function. 02:13.710 --> 02:20.820 Remember, the Marshall function encodes a value to Jason and the Marshall function because Jason to 02:20.820 --> 02:21.420 a value. 02:22.330 --> 02:29.830 It takes a bite slice and an empty interface and returns an error, the data parameter expects and encoded 02:29.830 --> 02:30.740 JSON data. 02:31.030 --> 02:37.810 So first, the commercial function will pass the JSON data and it's going to write the result to a variable 02:37.810 --> 02:40.220 that you're going to pass through the V parameter. 02:41.080 --> 02:45.850 So first I'm going to store the error in the air variable and I'm going to handle the error. 02:47.360 --> 02:49.880 Now, I'm going to give it the JSON data, Alexa. 02:50.450 --> 02:54.080 Lastly, I'm going to give it the user's slice here. 02:54.080 --> 02:58.690 The commercial function is going to decode the Jason into the user's slice. 02:59.330 --> 03:01.130 So let's print the result as the. 03:03.000 --> 03:04.480 Whoops, there is an error. 03:04.800 --> 03:12.690 It says the user is not a pointer, this happens because a function cannot change a variable directly. 03:13.080 --> 03:17.990 As you know, the user is a variable, so the function cannot change it. 03:18.450 --> 03:21.690 There is only one way for a function to change a variable. 03:22.110 --> 03:24.390 You need to use a pointer lachsa. 03:24.840 --> 03:29.610 Otherwise the a marginal function cannot store the decoder data in the user. 03:30.540 --> 03:32.070 Let me explain how it works. 03:33.970 --> 03:40.330 Think of this great as computer memory, each cell represents a memory location and each one has a memory 03:40.330 --> 03:40.860 address. 03:41.650 --> 03:43.510 Here is the user's variable. 03:43.930 --> 03:46.310 It is located on the memory location. 03:47.470 --> 03:49.930 Here is a call to the Marshall function. 03:50.230 --> 03:55.340 Now I'm going to pass the memory address of the user's variable to the, um, Marshall function. 03:55.900 --> 03:57.880 This is called the address operator. 03:58.240 --> 04:02.090 It finds the memory location of a value or a variable. 04:02.590 --> 04:09.730 So here it finds the memory address of the user's slice and it gives that address to the Marshall function 04:09.730 --> 04:10.280 LACHSA. 04:10.570 --> 04:16.210 So the function can directly go to the address of the user's variable and change it there. 04:16.570 --> 04:19.110 That's why you need to pass a pointer. 04:19.720 --> 04:25.690 Don't worry, I'm going to talk about pointers extensively in the upcoming sections, so you'd better 04:25.690 --> 04:26.940 understand how it works. 04:28.000 --> 04:33.880 Here is another problem, the Jason package can understand that there are three objects in the Jason 04:33.880 --> 04:36.350 data, but it couldn't mashallah. 04:36.550 --> 04:37.270 But why? 04:38.050 --> 04:40.410 Let's take a look at the Jason here. 04:40.420 --> 04:42.820 The field name is username Howar. 04:42.820 --> 04:45.010 In this truck, the field name is name. 04:45.380 --> 04:52.000 So you need to tell this to the Jason package so it can get data from the user name fields into the 04:52.000 --> 04:53.320 name field of the truck. 04:54.070 --> 04:56.590 To do that, you need to use field tax. 04:57.100 --> 05:04.120 So I'm going to add a field tag like so here I say decode the user name field in the JSON, object to 05:04.120 --> 05:06.040 the name field in the user's track. 05:06.870 --> 05:08.230 Let's take a look at the Jassam. 05:08.830 --> 05:12.220 It stores the permissions data with the person's name. 05:12.700 --> 05:16.390 So I'm going to add another field tag to the permissions field like so. 05:17.640 --> 05:19.010 Ask them how it works. 05:19.790 --> 05:22.470 Let's pray they printed to do that. 05:22.490 --> 05:26.750 I'm going to range over the users and I'm going to print the username. 05:29.350 --> 05:34.330 Next, I'm going to use a switch and I'm going to save the permissions to the variable. 05:35.610 --> 05:40.530 As you know, some users don't have any permissions, so let's check for that first. 05:41.740 --> 05:44.740 Here I'm going to say this user has no power. 05:45.220 --> 05:45.700 OK, good. 05:45.900 --> 05:48.940 Next, I'm going to check for the admin permission Lexar. 05:50.410 --> 05:52.900 Now, I'm going to say, is it just that? 05:53.960 --> 05:56.660 Lastly, I'm going to check for the right permission. 05:57.550 --> 05:59.170 And I'm going to say, Ken writes. 06:00.790 --> 06:05.640 I'm going to put a new line here so the loop will print the next user on the next line. 06:07.340 --> 06:07.920 Looks good. 06:08.460 --> 06:14.570 Lastly, let's move the marshal into the if statement doing so wasn't necessary, but I think it looks 06:14.570 --> 06:15.180 better to me. 06:15.650 --> 06:19.980 So you've learned how to encode and decode JSON data. 06:20.360 --> 06:23.550 You'll also learn about field text and pointers. 06:24.200 --> 06:27.070 This was the last section of the composite types. 06:27.260 --> 06:28.980 You completed all of them. 06:29.210 --> 06:30.500 Congrats, Volda. 06:30.980 --> 06:35.260 Now you deeply know how to represent data in the go programming language. 06:35.570 --> 06:36.390 That's awesome. 06:36.950 --> 06:37.490 All right. 06:37.850 --> 06:38.700 That's all for now. 06:39.080 --> 06:40.910 See you in the next part.