Go custom JSON: MarshalJSON, UnmarshalJSON and the alias trick

11 steps · about 3 h 5 min · Intermediate

  1. JSON and Go (article, The Go Blog, 20 min)

    The official starting point for Marshal, Unmarshal, type matching and the Encoder and Decoder.

  2. Go by Example: JSON (interactive, Go by Example, 10 min)

    Short runnable examples show struct tags and streaming before you customise anything.

  3. The Ultimate Guide to JSON in Go (guide, Boot.dev, 20 min)

    Covers the ignore tag and reading or writing JSON files, then gives a first custom marshaling example.

  4. Using JSON in Go: A guide with examples (article, LogRocket Blog, 20 min)

    Practises nested structs and tag options so the default behaviour is solid before you override it.

  5. How to use JSON with Go [best practices] (article, YourBasic Go, 15 min)

    Shows how time.Time, unexported fields and readers and writers behave by default.

  6. Go JSON: encoding/json, struct tags, json.RawMessage & Streaming (guide, Jsonic, 20 min)

    Explains when the Marshaler interface is called and why UnmarshalJSON needs a pointer receiver.

  7. JSON in Go — encoding/json, Struct Tags, Custom Marshaling & Performance (tutorial, DevToolsBuilder, 20 min)

    Has worked examples of a custom date format and enum-as-string, the two most common uses.

  8. How to Create Custom JSON Marshaler in Go (tutorial, OneUptime, 20 min)

    Adds Unix timestamps, priority enums and computed fields, so you can compare several patterns.

  9. Creating Custom Marshaling and Unmarshaling Logic in Go (article, Sling Academy, 10 min)

    Introduces the Alias type that stops MarshalJSON from calling itself.

  10. Handling JSON in Go (article, Homayoon Alimohammadi, 15 min)

    Finishes with an intermediate-struct UnmarshalJSON, wrapped errors and a file round trip.

  11. Go JSON Marshal & Unmarshal: Struct Tags (2026) (article, TopicTrick, 15 min)

    Closes with a Money type, telling a missing field from null, and Decoder versus Unmarshal for HTTP handlers.