Go encoding/json - Marshal, Unmarshal and struct field tags
Answer: Marshal returns the JSON encoding of v. Unmarshal parses JSON-encoded data and stores the result in a pointer. The json package reads and writes JSON in Go programs.
Examples
type Message struct {
Name string `json:"name"`
Body string `json:"body,omitempty"`
Time int64 `json:"time"`
}
b, err := json.Marshal(Message{Name: "Alice", Time: 1294706395881547000})
var m Message
if err := json.Unmarshal(b, &m); err != nil { ... }
dec := json.NewDecoder(r)
dec.UseNumber()
Rules
- Struct tags such as
json:"name,omitempty"rename a field or omit an empty value. - Only exported fields are encoded.
See also
Claims
- id: C1 claim: Marshal returns the JSON encoding of a value. source: S1 evidence: "Marshal returns the JSON encoding of v"
- id: C2 claim: Unmarshal parses JSON into a destination. source: S1 evidence: "Unmarshal parses the JSON-encoded data and stores the result"
- id: C3 claim: The json package reads and writes JSON in Go programs. source: S2 evidence: "With the json package it's a snap to read and write JSON"
Sources
- [S1] encoding/json package. pkg.go.dev. https://pkg.go.dev/encoding/json . License: BSD-3-Clause. Accessed 2026-11-18.
- [S2] JSON. Go blog. https://go.dev/blog/json . License: CC-BY-4.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: pkg.go.dev, encoding/json package; Go blog, JSON. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: pkg.go.dev, encoding/json package; Go blog, JSON. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. CC-BY-4.0 (https://go.dev/copyright). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go encoding/json - Marshal, Unmarshal and struct field tags." iinventedlight Library, iil:note:mdez5d6y, version of 2026-11-18. https://iinventedlight.com/library/notes/go-encoding-json/. License: CC-BY-4.0.
@misc{iil_note_mdez5d6y,
author = {{iinventedlight}},
title = {{Go encoding/json - Marshal, Unmarshal and struct field tags}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/go-encoding-json/},
note = {Permanent ID iil:note:mdez5d6y. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-encoding-json.md.