Go's standard library handles JSON with encoding/json and XML with encoding/xml. Both packages use struct tags to map Go fields to document names, and both offer a one-shot API (Marshal and Unmarshal) and a streaming API (Encoder and Decoder). Use the one-shot functions for small in-memory values and the streaming types for HTTP bodies, files and large inputs.
The two packages share the same design, so what you learn about tags, custom marshalers and error handling for JSON carries over to XML almost unchanged.
encoding/json
json.Encoder and json.Decoderencoding/xmlencoding/jsonjson.Marshal turns a Go value into a byte slice, and json.Unmarshal fills a pointer to a Go value from JSON bytes. Only exported fields (capitalised names) are serialised, and the json struct tag controls the key name.
The empty Email is dropped because of omitempty. Use json.MarshalIndent(u, "", " ") when you want readable output.
| Tag | Effect |
|---|---|
json:"name" | Use name as the JSON key |
json:"name,omitempty" | Skip the field when it has its zero value |
json:"-" | Never serialise the field |
json:",string" | Encode a number or bool as a JSON string |
json:"name,omitzero" | Skip the field when it is the zero value, including structs such as time.Time (Go 1.24 and later) |
Decoding matches keys case-insensitively, but you should still write tags explicitly so the wire format does not depend on field names.
When the shape is not known at compile time, decode into map[string]any or any. Numbers become float64, objects become map[string]any, and arrays become []any.
To defer decoding of one part, use json.RawMessage. It keeps the raw bytes so you can pick the target type after reading a discriminator field.
json.Encoder and json.DecoderFor HTTP handlers and files, wrap the io.Writer or io.Reader directly instead of building a full byte slice first.
DisallowUnknownFields rejects keys that have no matching field, which catches client typos. A Decoder can also read a stream of concatenated values by calling Decode in a loop until it returns io.EOF, which is how you process newline-delimited JSON.
Implement json.Marshaler and json.Unmarshaler (or encoding.TextMarshaler) when a type needs a custom wire format, for example a status enum stored as a string.
Because the type implements the text interfaces, encoding/json uses them for values and for map keys. Decoding errors can be inspected with errors.As:
encoding/xmlThe XML package follows the same model but its tags are richer: xml:"name" for an element, xml:"id,attr" for an attribute, xml:",chardata" for text content, and xml:"a>b" for nested paths.
For very large XML files, use xml.NewDecoder and call Token in a loop so you handle one element at a time instead of loading the whole document.
| Need | Approach |
|---|---|
| Public REST API | encoding/json with explicit tags and DisallowUnknownFields |
| Unknown schema | map[string]any or json.RawMessage |
| Large files or logs | Decoder and Encoder streaming |
| Legacy SOAP or feeds | encoding/xml |
| Hot path, millions of values | Benchmark first, then consider code generation or a third-party library |
| Internal binary RPC | Protocol Buffers or another binary format |
Go 1.25 added an experimental encoding/json/v2 behind GOEXPERIMENT=jsonv2. It is not stable, so production code should keep using encoding/json for now. The standard library is fast enough for most services, so measure before replacing it.
The field name starts with a lowercase letter, so it is unexported and encoding/json cannot see it. Capitalise it and add a tag if you want a lowercase key.
float64?Decode into a typed struct, or call dec.UseNumber() on a Decoder so numbers arrive as json.Number and keep full precision for large integers.
Marshal or Encoder?Use Marshal when you need the bytes in memory. Use json.NewEncoder(w) when writing to a response or file, since it avoids an extra copy and appends a newline.
Implement UnmarshalJSON on a small wrapper type, or decode into json.RawMessage and inspect the first byte.
Use encoding/json and encoding/xml with explicit struct tags, stream large or untrusted input with decoders, limit request size, and implement the marshaler interfaces for custom formats. Treat map[string]any as a fallback for unknown shapes, and benchmark before reaching for third-party serializers.