JSON is the de facto standard data transfer format for microservices and REST APIs. The aim of this video is to demonstrate how Go structs are marshalled and unmarshalled and how the JSON format can be customized using tags.
- [Instructor] Hi and welcome back on JSON Marshalling and Unmarshalling of Go Structs. So in this video we're going to take a look at how we can use the Go JSON package to marshal and unmarshal Go Structs to and from JSON. We also have a look at how we can define additional metadata to determine how your Go Structs will be marshaled to and from JSON, and finally we will add a simple REST endpoint with a JSON response to the HTTP server we implemented in the previous video.
So let's get started then and switch to our development environment. So I've prepared a few things. So what we want to do is implement a REST API for books. So let's start with the books. So what I've prepared here is a rough skeleton of a book structure. We want to implement a to JSON and a from JSON func and later on we want to implement the books and the function.
So let's start with the books. So let's define maybe a title, an author, and an ISBN. So we defined three fields in our book structure. So that's very straight forward, right. So what we're going to do next find a to JSON method for our book structure. So first of all, the JSON marshal method and pass in the book as a parameter, and it can respond with two possible outcomes.
In case of success this method will return a byte array the marshalled data and in case of an error we'd get a error structure. So what we will is first of all we check for any errors and since in any case everything went right we will respond with our to JSON structure. So let's see if this already works. So what we were using here is of course it will be test driven, so we will implement a small unit test for our function.
So let's define a very simple book in this line. Next we will issue the call to our to JSON method and take the response, which should be a byte array now. Now we will insert the response with some expected value, and this is what we do here. We call the equals method, we pass it the expected string value, we now pass it as a third parameter, the string of our JSON response and this here is the message in case of an error.
So let's see if this already works. So we will use and go test here. So alright, you see here that the test book to JSON method it passed already. So let's go back and implement the from JSON function. So first of all we will define an empty book.
In this line we will now unmarshal the data parameter we received into our book. Once again, we'll check for error and in case everything went right we simply return the unmarshalled book structure. So let's see if this works and implement the test for this function. So let's assume we have a byte array of a JSON structure.
So here, JSON string, and let me get the byte array from this one. Next up we will call our unmarshal message from JSON and we pass it our byte array. Finally, we want to check that everything worked okay. So let's see if this works. So all we do here is basically we call the equals method and we compare the expected book with the actual book we received.
So once again we call the go test method here and hooray, all the tests. Now let's assume that we don't like the format this is. So let's assume that we want to have everything marshalled and lowercase here. So we want lowercase title, lowercase author, lowercase ISBN, and same goes for this one here.
Now if we save this and we switch back here now we have failing tests, so this is not good. So what we're going to do here is we introduce additional metadata now to determine how Go will marshal and unmarshal our structures. So all we need to do here is define additional tags to our fields.
That will tell Go how the unmarshalling should be done. So basically what we do here is we say for the format of JSON we want this filed to be marshalled at lowercase title, and for the second field we want it to be marshalled at lowercase author, and for the third field we want it to be marshalled at lowercase ISBN. So if we go back to our test now we see we expect title and author and ISBN here and the same goes here.
So we didn't change the test only the structure. So let's see if our test is successful now. So here you see both of our tests now are successful. In the final showcase we now want to marshal and unmarshal our book structure using an HTTP request. So let's see how we can do this.
So let's define some books first, okay. So we define a slice here and we initialize it with two books. Next thing, go to our simple HTTP server and we define a new handle function for the path of API books.
In here the implementation is pretty straight forward now. First of all, we will take our book slice and marshal it to JSON. So we call JSON.marshal and we pass it the slice and either we get back the byte array in case of success or an error. Now we check for the error and then finally what we do next is, because we have JSON data now first of all, we need to tell the client about this fact.
So we add the content type header, pass it the value application JSON charset UTF 8, and then finally we will write back the response structure. So let's see if it worked. Our go build will compile everything. We start our HTTP server, we go to our REST client, and now we call local host 8080 or the API/books and if we press the send button now, hooray, here you go, look at this.
We have our two books marshalled in perfect JSON with the appropriate header, here you see the content type and a status code of 200. So this is a very basic get API for books. So in the next video we will extend this REST API with further functions and I'm going to show you how we can implement a very, very simple REST API for our books. I hope to see you then, bye-bye.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- Implementing Go HTTP Server
- JSON marshalling and unmarshalling of Go structs
- Implementing a simple REST API
- Using Docker workflows and commands
- Building a naïve Docker image
- Running a containerized Go microservice
- Kubernetes architecture and concepts
- Deploying a Go microservice to Kubernetes
- Implementing service descriptors
- Performing rolling updates