package main
import (
"encoding/json"
"fmt"
)
type info struct {
p person
}
type person struct {
Name string
}
func main() {
p1 := info{}
data := `{"p":{"Name": "John"}}`
json.Unmarshal([]byte(data) , &p1)
fmt.Println(p1.p.Name)
}
I am trying to save the name “John” in the Name field of p . But its not happening? Whats the error here ?
Since json.Unmarshal is in a different package, it does not have access to unexpected fields.
Also, while it would not have helped in this case, always check returned errors. Before I realized that info.p needed to be exported, I added error checking for json.Unmarshal. Finding it did not return an error told me the problem was not with json.Unmarshal so I looked at what it was given and found the unexported info.p.