type MiddleMan struct {
User CompletedByUser `json:"user"`
}
type CompletedByUser struct {
DisplayName string `json:"displayName"`
Id string `json:"id"`
}
Using the following types, I run the code
shorterJsonString := `{
"user":{
"displayName":null,
"id":"a03dfee5-a754-4eb9"
}
}`
if !json.Valid([]byte(shorterJsonString)) {
log.Println("Not valid")
}
var middleMan models.MiddleMan
newReader := strings.NewReader(shorterJsonString)
json.NewDecoder(newReader).Decode(&middleMan)
log.Println(middleMan)
Unfortunately, the decoder is seemingly broken for nested objects. Rather than spitting out actual objects, the print prints out
{{ a03dfee5-a754-4eb9 }}
It seems to flatten the whole thing into the id field. What is going on here?
middleMan, which you are printing, is of typemodels.MiddleMan. It has nothing to do with JSON, and thus will receive fmt-style formatting fromlog.Println, which likes to omit field names as well as null fields. You could get it to be slightly more verbose usinglog.Printf("%+v\n", middleMan). What are you trying to accomplish?