Go to JSON type mappings

suggest change

Mapping between Go types and JSON types:

JSON type Go type
null nil
boolean bool
number float64 or int
string string
array slice
dictionary map[struct]interface{} or struct

Here’s how they look in practice:

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

w := new(tabwriter.Writer)
w.Init(os.Stdout, 5, 0, 1, ' ', 0)
fmt.Fprint(w, "Go type:\tJSON value:\n")
fmt.Fprint(w, "\t\n")
printSerialized(nil, w)
printSerialized(5, w)
printSerialized(8.23, w)
printSerialized("john", w)
ai := []int{5, 4, 18}
printSerialized(ai, w)
a := []interface{}{4, "string"}
printSerialized(a, w)
d := map[string]interface{}{
	"i": 5,
	"s": "foo",
}
printSerialized(d, w)
s := struct {
	Name string
	Age  int
}{
	Name: "John",
	Age:  37,
}
printSerialized(s, w)
w.Flush()
Go type:                        JSON value:
                                
<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}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents