# JSON
JSON is quickly becoming the ubiquitous serialization format for web APIs, soit may be the most relevant when learning how to build web appsusing Go. Fortunately, Go makes it simple to work with JSON -- it isextremely easy to turn existing Go structs into JSON using the `encoding/json`package from the standard library.
~~~
package main
import (
"encoding/json"
"net/http"
)
type Book struct {
Title string `json:"title"`
Author string `json:"author"`
}
func main() {
http.HandleFunc("/", ShowBooks)
http.ListenAndServe(":8080", nil)
}
func ShowBooks(w http.ResponseWriter, r *http.Request) {
book := Book{"Building Web Apps with Go", "Jeremy Saenz"}
js, err := json.Marshal(book)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
~~~
## Exercises
1. Read through the JSON API docs and find out how to rename and ignore fields for JSON serialization.
1. Instead of using the `json.Marshal` method, try using the `json.Encoder` API.
1. Figure our how to pretty print JSON with the `encoding/json` package.
- Introduction
- 1. Go Makes Things Simple
- 2. The net/http package
- 3. Creating a Basic Web App
- 4. Deployment
- 5. URL Routing
- 6. Middleware
- 7. Rendering
- JSON
- HTML Templates
- Using The render package
- 8. Testing
- Unit Testing
- End to End Testing
- 9. Controllers
- 10. Databases
- 11. Tips and Tricks
- 12. Moving Forward