json.Marshal
The Go function that encodes a value, such as a struct or slice, into JSON bytes.
In more detail
It returns the bytes and an error, and uses struct tags for key names. Only exported fields are included. For output to a stream such as standard output, json.NewEncoder writes directly instead.
Example
report, err := json.Marshal(Report{Host: "api", Healthy: true})Related terms
- json.Unmarshal — The Go function that decodes JSON bytes into a Go value you pass by pointer, such as &reply.
- encoding/json — Go's standard library package for converting between Go values and JSON text.
- struct tag — A string in backquotes after a struct field, such as `json:"status"`, that tells encoders like encoding/json how to name or treat that field.
- json.Encoder — A Go value that writes JSON to an io.Writer, such as standard output, with optional indentation for human readers.
Taught in
- Parsing JSON with encoding/json — Concurrent Systems Programming with Go
- Exporting Results as JSON — Concurrent Systems Programming with Go