Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Encoding and Data Formats

Go json.Marshal() function

Reference for Go json.Marshal and json.MarshalIndent: convert structs, maps and slices to JSON, how each type is encoded, and when Marshal returns an error.

json.Marshal encodes a Go value as JSON and returns it as a []byte. It handles structs, maps, slices, arrays, pointers, interfaces, strings, numbers and booleans, following the rules in the struct tags of each field. json.MarshalIndent does the same but formats the output with line breaks and indentation, which is handy for logs and files people read.

Marshal follows pointers and interface values to the data they hold. A nil pointer, interface, map or slice becomes null. Marshal never mutates its argument.

Go
data, err := json.Marshal(v)

Try it

Go Structs, maps and slices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Pretty printing and failures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func Marshal(v any) ([]byte, error)
func MarshalIndent(v any, prefix, indent string) ([]byte, error)
Parameter Description
v The value to encode.
prefix (MarshalIndent) Text placed at the start of every line after the first. Usually "".
indent (MarshalIndent) Text repeated once per nesting level, such as " " or "\t".

Returns. The JSON bytes (without a trailing newline) and an error.

Errors. *json.UnsupportedTypeError for channels, functions and complex numbers; *json.UnsupportedValueError for NaN, +Inf, -Inf and cyclic data; *json.MarshalerError when a MarshalJSON or MarshalText method fails. A map key type that is not a string, integer or encoding.TextMarshaler is also an error.

Usage notes

  • Only exported fields are written. Use tags to rename (json:"user_id"), omit empties (omitempty) or skip (json:"-").
  • Embedded structs are flattened into the parent object unless they carry a tag name.
  • Strings are escaped for HTML by default: <, > and & become <, >, &. To turn that off, use Encoder.SetEscapeHTML(false).
  • Invalid UTF-8 in a string is replaced with the Unicode replacement character.
  • Floats are printed in the shortest form that round-trips, and integers above 2^53 are fine in Go but may lose precision in a JavaScript consumer.
  • Custom output: implement MarshalJSON() ([]byte, error) on the type (value receiver, so it also works for non-pointer values). The bytes must be valid JSON; Marshal compacts them.
  • Cyclic structures (a pointer loop) cause an error rather than infinite recursion.
  • Allocation: Marshal allocates a new slice per call; with Encoder you can write directly into a Writer and avoid the intermediate copy.