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
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
ruby
require 'json' require 'set' class SensitiveScrubber
Recursively scrubbing secrets from JSON
recursion
data-masking
pattern-matching
Intermediate
7 steps
go
func (h *TransactionHandler) ExportCSV(c *gin.Context) { ctx := c.Request.Context() filters := parseTransactionFilters(c)
Streaming a CSV export in Gin
streaming
csv-export
database-cursor
Intermediate
8 steps
go
package store import ( "database/sql"
Wrapping and inspecting errors in Go
error-handling
error-wrapping
sentinel-errors
Intermediate
8 steps
go
package main import ( "context"
Graceful HTTP shutdown in Go
graceful-shutdown
signals
goroutines
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.