Go serializes data to JSON with json.Marshal (or a streaming json.Encoder) and parses it back with json.Unmarshal (or json.Decoder), both from the standard encoding/json package. Struct tags such as json:"name,omitempty" control field names and omission, and only exported fields are encoded.
The sections below cover encoding, decoding, tags, custom marshalers, streaming and the pitfalls that cause most JSON bugs in Go services.
json.Marshal converts structs, maps, slices, strings, numbers and booleans to a JSON byte slice. Use json.MarshalIndent for human-readable output.
Email is omitted because it is empty and has omitempty. Tags is a nil slice, so it becomes null; initialize it with []string{} if you want [].
json.Unmarshal fills a value you pass by pointer. Field names match case-insensitively, but tags make the mapping explicit.
Unknown fields such as extra are ignored by default. To reject them, use a decoder with DisallowUnknownFields.
When the shape is not known, decode into map[string]any or any. JSON numbers become float64, objects become map[string]any and arrays become []any.
Use json.RawMessage to delay decoding part of a document, or a json.Decoder with UseNumber to keep large integers exact instead of converting them to float64.
| Tag | Effect |
|---|---|
json:"name" | Use name as the JSON key |
json:"name,omitempty" | Omit when the value is empty (false, 0, nil, empty string, slice, map) |
json:"name,omitzero" | Omit when the value is the zero value; added in Go 1.24, works well for time.Time |
json:"-" | Never encode or decode this field |
json:",string" | Encode a number or bool as a JSON string |
Unexported fields (lowercase names) are always skipped. Embedded structs have their fields promoted into the parent object.
For HTTP bodies and files, prefer Encoder and Decoder, which read and write a stream without building an intermediate byte slice.
The handler limits the body size, rejects unknown fields, returns 400 on bad input and sets the content type before writing. The POST /people pattern syntax requires Go 1.22 or newer.
Implement json.Marshaler and json.Unmarshaler when the default representation is not what you need. Keep it simple: here a duration is written as a string such as 1h30m0s.
If you wrap a struct inside its own MarshalJSON, use a local alias type (type alias Person) so the method does not call itself recursively. For text-like types, encoding.TextMarshaler is often simpler.
name string is silently skipped.Unmarshal, which returns an InvalidUnmarshalError.Marshal and Unmarshal.Marshal sorts map keys.float64 unless you decode into a typed field or use UseNumber.Recent Go releases ship an experimental, opt-in encoding/json/v2 behind a GOEXPERIMENT flag. Stable code should keep using encoding/json v1, which remains supported.
Marshal returns a byte slice in memory. NewEncoder(w).Encode(v) writes to an io.Writer and appends a newline, which suits HTTP responses and files.
They are probably unexported. Only fields starting with an uppercase letter are encoded.
Use the tag json:"-", or omitempty or omitzero to omit it only when it is empty.
Create a json.Decoder and call DisallowUnknownFields() before Decode.
encoding/json turns Go values into JSON with Marshal or Encoder and back with Unmarshal or Decoder. Use struct tags to control names and omission, stream request bodies with size limits, implement custom marshalers sparingly, and always handle the returned errors.