Transcript from the "JSON Parsing with Unmarshal" Lesson
>> So we need to parse JSON, okay? So fortunately Go includes JSON parsing and unparsing, because it includes HTTP, and also Go includes all the JSON management that we need. So for that we can go here, and now instead of printing the JSON, I need to do something else with it, okay?
[00:00:22] So for working with JSON, I could create a new a new function if you want, the function is getting large, but let's start with here and then we can refactor it. So I need to talk with a JSON package. So you type json, lowercase, packages are typically lowercase, so lowercase json., and by the way, we have a problem using json, why?
[00:00:48] Because that's the name of my variable as well. And in this case, my variable is overriding package names. So that's a tip. You shouldn't use variable names that are packages here, but there are solutions also to that, because you can add an alias to a package. But yeah, it's probably better to just change your variable, okay?
[00:01:13] In fact, you will see that I won't actually even need this, okay? So, I'm going to delete the string, you will see that I don't need the string of the JSON. I will put that to the console. So, now I will try again json, and json, they have a couple of methods.
[00:01:32] We have encoders and decoders, in case you wanna get deep into the process of encoding or decoding the JSON, but for simple things, you don't need to create a decoder or an encoder. Remember, is this a constructor? Here's a question for you, parentheses, question. NewDecoder, NewEncoder, what is that?
[00:01:56] Is that a constructor? That's a factory. That's a factory. Remember, a factory is a design pattern. So the constructor is not a method or a function, a constructor is always when you use the type, and then you use curly braces, okay? So that's actually a factory. But what we have is the functions marshal and unmarshal.
[00:02:20] Unmarshal will parse a JSON and will store that into an instance, into a value that you parse as an argument. So this is like the decoding. This is the one that is parsing, and it receives a slice of bytes. That's why I said that, I don't actually need to convert it into a string.
[00:02:41] I can just take the bodyBytes that I have and unmarshal them. Marshall because had to do with the shaping, okay? It's using that definition of marshalling to marshal, it has to do with shape. So we are shaping the data that we have in JSON into another structure. And then I need to parse any, just any, it's a value.
[00:03:24] So that's the term that we use in Go, value. So we think you'll parse a value, okay? So the idea is that it's not going to return me an object, in fact, if you look the Unmarshal, it returns only a possible error in case it couldn't decode that file.
[00:03:42] And by the way, look at these, just completely off, but not off topic, but not related to this one. When I'm getting into the Unmarshal, remember this is Ctrl+click or Cmd+click if you're on a Mac, you're getting into the source code of Go. And this is the Unmarshal function within the JSON package, but the name of the file is actually decode.
[00:04:01] Do we use the naming of the file anywhere? No, it's just how they organized the package. But the decode name we don't use it, it's not the function name, it's not the package name. We don't care about it, okay? Closing parenthesis. So I'm going to Unmarshal this, and, yeah, I received a possible error here, but not the data itself.
[00:04:27] In this case, we need to parse the data as an argument. So, for example, I need to create first a variable that, I can call this my cryptoRate of type, Remember the package name, datatypes.Rate, okay? And then I need to kind of parse the variable like this. However, this is not gonna work as it is, why?
[00:05:02] Because what happens when I parse a variable to a function?
>> It clones it.
>> It clones it. It creates a copy. So if I want to parse a variable and I want the function to change the contents of that variable, I need to parse the address of that variable.
[00:05:25] Okay, makes sense? Then I should check if there is an error, it means that it's not a JSON, the JSON is not valid, something happened, the structure is not matching the JSON, something like that. So I should return nil and an error. Also, look at this, it's complaining that I shouldn't create new variables on left side of blah, blah, blah.
[00:05:57] So what's going on here? That at the same code block, I already have another err variable. So I shouldn't use colon equals. If I want, I can reuse the same one, the same variable, but I shouldn't create a new one. It didn't happen before because we were in a different code block.
[00:06:49] But the problem is that, is the API matching my structure? So let's see this in action. So if I open Google Chrome and I use BTC here, this is the current structure.
>> There's nothing called price.
>> Yeah, there's nothing called price. I can open this here in the new file and format it as JSON, so we can see this better.
[00:07:23] So let's compare that with my data type. I don't want that, I wanna send that to the right, there we are. So yeah, we don't have a currency, we don't have a price, so we have a problem, because I'm trying to convert that JSON into that structure and it's not working, okay?
[00:07:49] So, and that's typically something normal. I typically receive data from a web service that is not mapping directly to my data model. So I need to make some kind of conversion here, okay? Makes sense? So I typically need to create another data type. Typically, it's part of the API, so not from part of my model.
[00:08:12] So I need to create another file here for the data types of the API or the responses. Okay, so we can call these responses.go. Does that make sense? So package, what's the package? package api. And I need to somehow match this. Where is it? Here. But don't worry, you shouldn't be typing a lot because we will see another solution.
[00:08:42] But anyway, let me start working with this.