Convert Go map to json

JsonGo

Json Problem Overview


I tried to convert my Go map to a json string with encoding/json Marshal, but it resulted in a empty string.

Here's my code :

package main

import (
	"encoding/json"
	"fmt"
)

type Foo struct {
	Number int    `json:"number"`
	Title  string `json:"title"`
}

func main() {
	datas := make(map[int]Foo)

	for i := 0; i < 10; i++ {
		datas[i] = Foo{Number: 1, Title: "test"}
	}

	jsonString, _ := json.Marshal(datas)

	fmt.Println(datas)
	fmt.Println(jsonString)
}

My output is :

map[9:{1 test} 2:{1 test} 7:{1 test} 3:{1 test} 4:{1 test} 5:{1 test} 6:{1 test} 8:{1 test} 0:{1 test} 1:{1 test}]

[]

I really don't know where I'm wrong. Thank you for your help.

Json Solutions


Solution 1 - Json

If you had caught the error, you would have seen this:

jsonString, err := json.Marshal(datas)
fmt.Println(err)

// [] json: unsupported type: map[int]main.Foo

The thing is you cannot use integers as keys in JSON; it is forbidden. Instead, you can convert these values to strings beforehand, for instance using strconv.Itoa.

See this post for more details: https://stackoverflow.com/a/24284721/2679935

Solution 2 - Json

It actually tells you what's wrong, but you ignored it because you didn't check the error returned from json.Marshal.

json: unsupported type: map[int]main.Foo

JSON spec doesn't support anything except strings for object keys, while javascript won't be fussy about it, it's still illegal.

You have two options:

1 Use map[string]Foo and convert the index to string (using fmt.Sprint for example):

datas := make(map[string]Foo, N)

for i := 0; i < 10; i++ {
	datas[fmt.Sprint(i)] = Foo{Number: 1, Title: "test"}
}
j, err := json.Marshal(datas)
fmt.Println(string(j), err)

2 Simply just use a slice (javascript array):

datas2 := make([]Foo, N)
for i := 0; i < 10; i++ {
	datas2[i] = Foo{Number: 1, Title: "test"}
}
j, err = json.Marshal(datas2)
fmt.Println(string(j), err)

playground

Solution 3 - Json

This behaviour has been changed over time. I am using go v1.16.5 and I can happily pass int type in as JSON key. I have tried the same problem now and I can see the below result. Type conversion of a non-string key has been added by textMarshaler and textUnmarshaler interfaces. For more info, you can visit this https://golang.org/pkg/encoding/json/#Marshal

type Foo struct {
	Number int    `json:"number"`
	Title  string `json:"title"`
}

datas := make(map[int]Foo)

for i := 0; i < 5; i++ {
	datas[i] = Foo{Number: 1, Title: "test"}
}

jsonString, _ := json.Marshal(datas)

fmt.Println("Datasets Result : ", datas)
fmt.Println("Marshal Datasets Result : ", string(jsonString), err)

m := make(map[int]Foo)
err = json.Unmarshal(jsonString, &m)
if err != nil {
	panic(err)
}

fmt.Println("Unmarshal JSON Result : ", m)

Output:

Datasets Result map : [0:{1 test} 1:{1 test} 2:{1 test} 3:{1 test} 4:{1 test}]

Marshal Datasets Result : {"0":{"number":1,"title":"test"},"1":{"number":1,"title":"test"},"2":{"number":1,"title":"test"},"3":{"number":1,"title":"test"},"4":{"number":1,"title":"test"}} <nil>

Unmarshal JSON Result :  map[0:{1 test} 1:{1 test} 2:{1 test} 3:{1 test} 4:{1 test}]

Solution 4 - Json

Since this question was asked/last answered, support for non string key types for maps for json Marshal/UnMarshal has been added through the use of TextMarshaler and TextUnmarshaler interfaces here. You could just implement these interfaces for your key types and then json.Marshal would work as expected.

package main

import (
	"encoding/json"
	"fmt"
	"strconv"
)

// Num wraps the int value so that we can implement the TextMarshaler and TextUnmarshaler 
type Num int

func (n *Num) UnmarshalText(text []byte) error {
	i, err := strconv.Atoi(string(text))
	if err != nil {
		return err
	}
	*n = Num(i)
	return nil
}

func (n Num) MarshalText() (text []byte, err error) {
	return []byte(strconv.Itoa(int(n))), nil
}

type Foo struct {
	Number Num    `json:"number"`
	Title  string `json:"title"`
}

func main() {
	datas := make(map[Num]Foo)

	for i := 0; i < 10; i++ {
		datas[Num(i)] = Foo{Number: 1, Title: "test"}
	}

	jsonString, err := json.Marshal(datas)
	if err != nil {
		panic(err)
	}

	fmt.Println(datas)
	fmt.Println(jsonString)

	m := make(map[Num]Foo)
	err = json.Unmarshal(jsonString, &m)
	if err != nil {
		panic(err)
	}

	fmt.Println(m)
}

Output:

map[1:{1 test} 2:{1 test} 4:{1 test} 7:{1 test} 8:{1 test} 9:{1 test} 0:{1 test} 3:{1 test} 5:{1 test} 6:{1 test}]
[123 34 48 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 49 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 50 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 51 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 52 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 53 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 54 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 55 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 56 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 44 34 57 34 58 123 34 110 117 109 98 101 114 34 58 34 49 34 44 34 116 105 116 108 101 34 58 34 116 101 115 116 34 125 125]
map[4:{1 test} 5:{1 test} 6:{1 test} 7:{1 test} 0:{1 test} 2:{1 test} 3:{1 test} 1:{1 test} 8:{1 test} 9:{1 test}]

Solution 5 - Json

data := map[string]interface{}
jsonByte, err := json.Marshal(data)
if err != nil{
    log.Fatal(err)
}
jsonString := string(jsonByte)
fmt.Println(jsonString) // will be printing data map in json formate.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCronos87View Question on Stackoverflow
Solution 1 - JsonjuliencView Answer on Stackoverflow
Solution 2 - JsonOneOfOneView Answer on Stackoverflow
Solution 3 - JsonkamalView Answer on Stackoverflow
Solution 4 - JsonvimView Answer on Stackoverflow
Solution 5 - JsonAman AgarwalView Answer on Stackoverflow