Module lessons (5/5)
`encoding/json`
json.Marshal/Unmarshal, struct tag, json.Decoder/Encoder per stream, parsing errors.
What you'll learn in this lesson
- Serialize and deserialize Go values with
json.Marshalandjson.Unmarshal. - Control key names and omission of zero values via struct tag
json:"...". - Quickly identify why unexported fields silently disappear from JSON.
- Decode JSON stream with
json.NewDecoderand reject unexpected keys withDisallowUnknownFields().
Overview
Working with REST APIs, webhooks, and configurations in Go revolves around encoding/json. The package uses runtime reflection along with struct tags to translate data between JSON and strongly typed structs, but has strict rules about visibility and pointers.
The two main operations map Go to []byte JSON and vice versa. Remember that json.Unmarshal modifies the recipient, so it always requires a pointer:
Sections covered in this lesson
- 1
MarshalandUnmarshal - 2Tag Rules
json:"..." - 3Visibility: Exported fields only
- 4Streaming with
json.NewDecoderand rigorous validation - 5Deferred decoding with
json.RawMessage - 6Guided practice
Unlock "`encoding/json`" and the full Course Go curriculum
`json.Marshal/Unmarshal`, struct tag, `json.Decoder/Encoder` per stream, parsing errors.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Go plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.