Skip to main content
Module 8 · Lesson 5 of 540/50 in the course~14 min
Module lessons (5/5)

`encoding/json`

Lesson preview5 hands-on exercises readyCourse Go · Module 8 — Essential standard library

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.Marshal and json.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.NewDecoder and reject unexpected keys with DisallowUnknownFields().

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. 1Marshal and Unmarshal
  2. 2Tag Rules json:"..."
  3. 3Visibility: Exported fields only
  4. 4Streaming with json.NewDecoder and rigorous validation
  5. 5Deferred decoding with json.RawMessage
  6. 6Guided practice
Pro Lesson · Module 8 — Essential standard library

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.