Essential Go JSON  Suggest an edit

Go to JSON type mappings

This is the mapping between Go types and JSON types:

JSON TypeGo Concrete Type
booleanbool
numberfloat64 or int
stringstring
arrayslice
dictionarymap[struct]interface{} or struct
nullnil

Here’s how they look in practice:

func printSerialized(v interface{}) {
	d, err := json.Marshal(v)
	if err != nil {
		log.Fatalf("json.Marshal failed with '%s'\n", err)
	}
	fmt.Printf("%T: %s\n", v, string(d))
}

printSerialized(nil)
printSerialized(5)
printSerialized(8.23)
printSerialized("john")
ai := []int{5, 4, 18}
printSerialized(ai)
a := []interface{}{4, "string"}
printSerialized(a)
d := map[string]interface{}{
	"i": 5,
	"s": "foo",
}
printSerialized(d)
s := struct {
	Name string
	Age  int
}{
	Name: "John",
	Age:  37,
}
printSerialized(s)
<nil>: null
int: 5
float64: 8.23
string: "john"
[]int: [5,4,18]
[]interface {}: [4,"string"]
map[string]interface {}: {"i":5,"s":"foo"}
struct { Name string; Age int }: {"Name":"John","Age":37}

  ↑ ↓ to navigate     ↵ to select     Esc to close