Transcript from the "Getting Nested JSON Data" Lesson
>> You'll see we're getting kind of a gap though because we have this URL. We have the name of Darth Vader here. We have our Homeworld URL as a string. But the actual planet is coming back as an empty struct. This kind of makes sense because we don't have that data at all.
[00:00:17] We have to make a separate request to actually get access to the planet information. So we need to add a few more things to our people function here. So for every person that we have, every person in our array. We need to go dig into that URL and make a separate get request.
[00:00:35] So the first thing we're gonna do is start a for loop. We're not gonna use the index. I'm gonna use the abbreviation pers for each individual person in our array. And then we're gonna go back to our range keyword. And we're iterating over our people variable. Which represents our whole all people struct.
[00:00:56] And the part of that struct we care about is the array of people which we called capital P, People. So for each of the person struct within our all people struct, we want to call that person. And then let's create a new function. We're gonna say, Get Homeworld.
[00:01:23] We're gonna make this a method. So on a person struct, we're going to create a method called get Homeworld. That's going to handle fetching this data for each person. And then just for visibility, let's assume this all goes great. We're going to just print line what that person now looks like.
[00:01:39] Cool, so let's go up above our get people function. And we'll create a new one. And so we're using a method and so the method needs a receiver. The thing we're gonna pass in and modify permanently is a pointer. Oops, sorry, not the word pointer, a pointer type of our person struct.
[00:02:05] We named our method get Homeworld. So within here we need to do some similar logic like we did with get people. The first thing we want to do is grab the response from http.Get. And instead of hard coding in that base url slash people. We need to know what is the Homeurl of that person start.
[00:02:29] So we're going to pass it p.H Homeurl. So this guy is coming off of the struct type that we have here in our person. And then again, if error does not equal nil I'll just print that guy out. There fetching Homeworld. And print the error. And then because our response is going to be that similar set of bytes, we'll grab that.
[00:03:04] And the error if we get one. And set that to our ioutil.Read all that response of the response body Once again now we're working with our second error within the same function block. So I'm gonna throw it in and if block. If define some variables, set them to the result of a function if there's an error We want to, sorry if error does not equal null.
[00:03:42] So over here error does not equal nil. We'll just keep using log here and log dot print. Error reading, response body in the error. Then we're gonna want to call Json dot Unmarshal. Are bytes and then sending them it to p.Homeworld. Person yeah p, sorry. Cool, sweet. So the beauty of using the F block to hang on to that error variable is that we get it scoped within our F block.
[00:04:36] But on the flip side now we're trying to access that bytes variable. Which is also, nested within our F block. So we can set up a VAR, bytes, okay?
>> Perfect, cool.
>> You'll just to have to get the colon out I think. Yes.
>> Cool, thank you.
[00:05:13] Okay so we're storing our We're going to set an initial value here to a variable called bytes. Which type is going to be an array of bytes coming back from my read all function. And then once we get it back and there's no error. We're going to send it through JSON data Marshall and then update our P for person.
[00:05:38] That we sent through here, of our method. So we'll save all of this. Let's restart our server one more time. We've refreshed the browser and now you're seeing that printout of our person strux. We have Luke Skywalker, the URL to where we can get that planet information. And then now our planet struck is actually populated with data.
[00:06:14] Cool, so kind of a similar exercise to a previous one in that we are defining some strux. But in this example, we got to write a method and hit an external API and manage parsing JSON. Including using some of those JSON tags that allow us to map the data that we have in our go.
[00:06:31] Using go conventional syntax with capital letters. And what actually lives on our JSON object and kind of navigate between the two.