Managing JSON data
JSON is the acronym for JavaScript Object Notation and, like the name implies, it's natively JavaScript. It has become very popular and it's the most used format for communication today. Go has very good support for JSON serialization/deserialization with the JSON
package that does most of the dirty work for you. First of all, there are two concepts to learn when working with JSON:
- Marshal: When you marshal an instance of a structure or object, you are converting it to its JSON counterpart.
- Unmarshal: When you are unmarshaling some data, in the form of an array of bytes, you are trying to convert some JSON-expected-data to a known struct or object. You can also unmarshal to a
map[string]interface{}
in a fast but not very safe way to interpret the data as we'll see now.
Let's see an example of marshaling a string:
import (
"encoding/json"
"fmt"
)
func main(){
packt := "packt"
jsonPackt, ok := json.Marshal(packt)
if !ok {
panic("Could not marshal object")
}
fmt.Println(string(jsonPackt))
}
$ "pack"
First, we have defined a variable called packt
to hold the contents of the packt
string. Then, we have used the json
library to use the Marshal
command with our new variable. This will return a new bytearray
with the JSON and a flag to provide and boolOK
result for the operation. When we print the contents of the bytes array (previous casting to string) the expected value appears. Note that packt
appeared actually between quotes as the JSON representation would be.
The encoding package
Have you realized that we have imported the package encoding/json
? Why is it prefixed with the word encoding
? If you take a look at Go's source code to the src/encoding
folder you'll find many interesting packages for encoding/decoding such as, XML, HEX, binary, or even CSV.
Now something a bit more complicated:
type MyObject struct {
Number int
`json:"number"`
Word string
}
func main(){
object := MyObject{5, "Packt"}
oJson, _ := json.Marshal(object)
fmt.Printf("%s\n", oJson)
}
$ {"Number":5,"Word":"Packt"}
Conveniently, it also works pretty well with structures but what if I want to not use uppercase in the JSON data? You can define the output/input name of the JSON in the structure declaration:
type MyObject struct {
Number int
Word string
}
func main(){
object := MyObject{5, "Packt"}
oJson, _ := json.Marshal(object)
fmt.Printf("%s\n", oJson)
}
$ {"number":5,"string":"Packt"}
We have not only lowercased the names of the keys, but we have even changed the name of the Word
key to string.
Enough of marshalling, we will receive JSON data as an array of bytes, but the process is very similar with some changes:
type MyObject struct { Number int`json:"number"` Word string`json:"string"` } func main(){ jsonBytes := []byte(`{"number":5, "string":"Packt"}`) var object MyObject err := json.Unmarshal(jsonBytes, &object) if err != nil { panic(err) } fmt.Printf("Number is %d, Word is %s\n", object.Number, object.Word) }
The big difference here is that you have to allocate the space for the structure first (with a zero value) and the pass the reference to the method Unmarshal
so that it tries to fill it. When you use Unmarshal
, the first parameter is the array of bytes that contains the JSON information while the second parameter is the reference (that's why we are using an ampersand) to the structure we want to fill. Finally, let's use a generic map[string]interface{}
method to hold the content of a JSON:
type MyObject struct { Number int `json:"number"` Word string `json:"string"` } func main(){ jsonBytes := []byte(`{"number":5, "string":"Packt"}`) var dangerousObject map[string]interface{} err := json.Unmarshal(jsonBytes, &dangerousObject) if err != nil { panic(err) } fmt.Printf("Number is %d, ", dangerousObject["number"]) fmt.Printf("Word is %s\n", dangerousObject["string"]) fmt.Printf("Error reference is %v\n", dangerousObject["nothing"]) } $ Number is %!d(float64=5), Word is Packt Error reference is <nil>
What happened in the result? This is why we described the object as dangerous. You can point to a nil
location when using this mode if you call a non-existing key in the JSON. Not only this, like in the example, it could also interpret a value as a float64
when it is simply a byte
, wasting a lot of memory.
So remember to just use map[string]interface{}
when you need dirty quick access to JSON data that is fairly simple and you have under control the type of scenarios described previously.