Go custom JSON: MarshalJSON, UnmarshalJSON and the alias trick
11 steps · about 3 h 5 min · Intermediate
- JSON and Go (article, The Go Blog, 20 min)
The official starting point for Marshal, Unmarshal, type matching and the Encoder and Decoder.
- Go by Example: JSON (interactive, Go by Example, 10 min)
Short runnable examples show struct tags and streaming before you customise anything.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Creating Custom Marshaling and Unmarshaling Logic in Go (article, Sling Academy, 10 min)
Introduces the Alias type that stops MarshalJSON from calling itself.
- Handling JSON in Go (article, Homayoon Alimohammadi, 15 min)
Finishes with an intermediate-struct UnmarshalJSON, wrapped errors and a file round trip.
- 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.