I can't decode the m into values even though it's a nested map and I provided struct filed of type map. I don't understand why ?
most of the problems was in the struct fields I didn't capitalize it's names so they were not exported
id int //it should be Id
values map[interface{}]interface{}//should be Values
and the map keys shouldn't be of type interface it should be of type string
Values map[interface{}]interface{}
//should be Values map[string]interface{}
the filed name didn't match the json keys you can solve this problem using the struct tags or by matching them with json keys
//the key for the embedded object was m .
// was trying to decode it into struct
//field and it's name was m .it was like this before fixing it
type edit struct{
Id int
Values [interface{}]interface{}
}
//and json object was like this
{
"id":5,
"m": {"name":"alice"}
}
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
)
type edit struct {
Id int
Values map[string]interface{}
}
func main() {
a := `{
"id":5,
"values":{
"name":"alice"
}
}`
var s edit
dec := json.NewDecoder(strings.NewReader(a))
err := dec.Decode(&s)
if err != nil {
log.Fatal(err)
}
fmt.Println(s)
}