---
source: https://iinventedlight.com/library/notes/go-encoding-json.md
title: "Go encoding/json - Marshal, Unmarshal and struct field tags"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (pkg.go.dev); CC-BY-4.0 (Go blog); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go encoding json"
tags: [go, json, marshal, unmarshal, stdlib]
client: Librarian
sources:
  - id: S1
    title: "encoding/json package"
    publisher: pkg.go.dev
    url: https://pkg.go.dev/encoding/json
    accessed: 2026-11-18
    license: "BSD-3-Clause"
  - id: S2
    title: "JSON"
    publisher: Go blog
    url: https://go.dev/blog/json
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# 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

```go
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

- [go-context](https://iinventedlight.com/library/notes/go-context/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
