Go Tutorial: JSON in Go
Speak the language of the web. Marshal structs to JSON, unmarshal JSON to structs, and build API-ready responses.
The language every API speaks
JSON is everywhere. Every web API sends and receives it. Every config file uses it. Every mobile app consumes it.
Go's `encoding/json` package handles it all — serialize a struct to JSON, parse JSON back into a struct, stream large JSON files, and customize exactly how your types are represented.
No third-party library needed. Twelve steps.
What you'll learn in this Go json in go tutorial
This interactive Go tutorial has 8 hands-on exercises. Estimated time: 22 minutes.
- What is JSON — JSON is the universal language of web APIs. `json.Marshal` turns a Go struct into JSON bytes. Run this and see a struct …
- Struct tags — control the JSON keys — API consumers expect lowercase JSON keys. Add `json:"..."` tags to all three fields of `Book` so the output uses lowerca…
- omitempty — skip empty fields — `omitempty` tells the JSON encoder to skip a field if it is its zero value. Add `omitempty` to the `Description` and `Ed…
- Unmarshal — JSON to struct — `json.Unmarshal` parses JSON bytes and fills a Go struct. Unmarshal the second JSON string into a `Book` and print its t…
- Nested structs — author details — Nested Go structs become nested JSON objects automatically. Add an `AuthorInfo` struct with `FirstName`, `LastName`, and…
- Maps — dynamic catalog metadata — `map[string]T` becomes a JSON object with dynamic keys. Add an entry `"language": "en"` to the metadata map and see it a…
- MarshalIndent — pretty print library inventory — `json.MarshalIndent` produces human-readable JSON with line breaks and indentation. Switch from `json.Marshal` to `json.…
- Build a library catalog search response — Build a reusable catalog response wrapper: define a `CatalogResponse` struct with `Status`, `Query`, `Count`, and `Books…
Go JSON in Go concepts covered
- The language every API speaks