← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:mdez5d6y

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.},
}

Markdown

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.