I am trying to parse given API response into a struct. It seems to be an array.
[
{
"host_name" : "hostname",
"perf_data" : "",
"plugin_output" : "All good",
"state" : 0
}
]
I cannot figure out how to create struct for it, I came up with:
type ServiceData struct {
HostName string `json:"host_name"`
PerfData string `json:"perf_data"`
PluginOutput string `json:"plugin_output"`
State int `json:"state"`
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
jsonStr := buf.String()
var servicedata ServiceData
json.Unmarshal([]byte(jsonStr), &servicedata)
But no luck.
Should I perhaps remove square brackets from the initial response? Could somebody point me in the right direction?
ServiceDatafixes the issue as per the answer below.