Handling Nested JSON Data in Go with Custom Structs - Unexpected Unmarshal Behavior
I'm getting frustrated with I've tried everything I can think of but I recently switched to I'm building a feature where I'm stuck on something that should probably be simple..... I'm working with a Go application that needs to parse nested JSON data from an external API response. The JSON structure looks like this: ```json { "user": { "id": 123, "name": "John Doe", "addresses": [ { "city": "New York", "zip": "10001" }, { "city": "Los Angeles", "zip": "90001" } ] } } ``` I defined the following Go structs to match this JSON structure: ```go type Address struct { City string `json:"city"` Zip string `json:"zip"` } type User struct { ID int `json:"id"` Name string `json:"name"` Addresses []Address `json:"addresses"` } type ApiResponse struct { User User `json:"user"` } ``` When I attempt to unmarshal the JSON into my structs using the `json.Unmarshal` method, I get the following behavior: ``` json: want to unmarshal string into Go struct field Address.city of type main.Address ``` I double-checked the JSON structure and the struct tags, and they seem to match correctly. Here's the code block where I'm performing the unmarshal: ```go package main import ( "encoding/json" "fmt" "log" ) func main() { jsonData := `{"user":{"id":123,"name":"John Doe","addresses":[{"city":"New York","zip":"10001"},{"city":"Los Angeles","zip":"90001"}]}}` var response ApiResponse err := json.Unmarshal([]byte(jsonData), &response) if err != nil { log.Fatalf("behavior unmarshaling JSON: %v", err) } fmt.Printf("Parsed User: %+v\n", response.User) } ``` I've already tried running this code with different JSON strings and simplifying the structs, but I keep getting the same behavior. Is there something I'm missing regarding the JSON structure or the way I'm defining my structs? Any help would be greatly appreciated! What's the best practice here? How would you solve this? For reference, this is a production mobile app. Is there a better approach? The stack includes Go and several other technologies. What are your experiences with this? I'm working with Go in a Docker container on Ubuntu 20.04.