Decoding JSON
When we talk about decoding JSON, what we are stating is that we are taking a JSON data structure and converting it into a Go data structure. Converting the JSON into a Go data structure gives us the benefit of working with the data natively. For example, if the JSON data has a field that is an array in Go, that would get decoded to a slice. We will then be able to treat that slice as we would any other slice, meaning we can iterate over the slice using a range
clause, we can get the length of the slice, append to the slice, and so on.
If we know what our JSON looks like ahead of time, we can use structs when parsing the JSON. Using Go terms, we need to be able to unmarshal
the JSON-encoded data and store the results in the struct. To be able to do this, we will need to import the encoding/json
package. We will be using the JSON Unmarshal
function. Unmarshaling is the process of parsing JSON to a data structure. Often, you will hear unmarshaling and decoding used interchangeably...