go
45 lines · 5 steps
Custom JSON marshaling in Go
How implementing MarshalJSON lets you reshape Go structs into exactly the JSON you want.
Explained by
highlit
1package model
2
3import (
4 "encoding/json"
5 "strings"
6 "time"
7)
8
9type Money int64
10
11type Order struct {
12 ID string
13 Customer string
14 Total Money
15 Tags []string
16 CreatedAt time.Time
17 ShippedAt *time.Time
18}
19
20func (m Money) MarshalJSON() ([]byte, error) {
21 dollars := float64(m) / 100
22 return json.Marshal(dollars)
23}
24
25func (o Order) MarshalJSON() ([]byte, error) {
26 type alias struct {
27 ID string `json:"id"`
28 Customer string `json:"customer"`
29 Total Money `json:"total"`
30 Tags string `json:"tags"`
31 CreatedAt string `json:"created_at"`
32 Shipped bool `json:"shipped"`
33 ShippedAt *time.Time `json:"shipped_at,omitempty"`
34 }
35
36 return json.Marshal(alias{
37 ID: o.ID,
38 Customer: o.Customer,
39 Total: o.Total,
40 Tags: strings.Join(o.Tags, ","),
41 CreatedAt: o.CreatedAt.UTC().Format(time.RFC3339),
42 Shipped: o.ShippedAt != nil,
43 ShippedAt: o.ShippedAt,
44 })
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing MarshalJSON on a named type customizes its JSON representation everywhere it appears.
- 2A local alias struct lets you reshape fields and tags without recursing into the original type's marshaler.
- 3Derived fields like a boolean flag or formatted timestamp can be computed at serialization time.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
go
package streaming import ( "bufio"
Streaming NDJSON logs over HTTP in Go
http-streaming
channels
select
Advanced
10 steps
go
package api import ( "crypto/sha256"
ETag conditional requests in Gin
http-caching
etag
conditional-requests
Intermediate
6 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
go
package logging import ( "context"
Deduplicating log attributes in Go's slog
decorator-pattern
structured-logging
immutability
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/custom-json-marshaling-in-go-explained-go-cec1/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.