Long time listener, first time caller!
I'm a beginner with Go, but have several years of Python experience. So, please forgive my ignorance.
So, I am trying to parse a multi-level json response from a web server into map[string]interface{} type.
Example:
func GetResponse() (statusCode int, responseData map[string]interface{}){
host := "https://somebogusurl.com/fake/json/data"
request, err := http.NewRequest("GET", host, nil)
if err != nil {
panic(err)
}
client := http.Client{}
response, err := client.Do(request)
if err != nil {
panic(err)
}
defer response.Body.Close()
data, _ := ioutil.ReadAll(response.Body)
json.Unmarshal([]byte(data), &responseData)
return response.StatusCode, responseData
}
func main() {
statusCode, data := GetResponse()
fmt.Println(data["foo"].(map[string]interface{})["bar"])
}
As you can see, I am trying to access the nested values in this new map that is returned. However, I get the following error:
panic: interface conversion: interface {} is []interface {}, not map[string]interface {}
I believe I am trying to access nested data which is in an interface, not a map[string]interface based on the error. Any idea how I access nested data?