I am playing around with Golang and JSON trying to do some calculations with data extracted from Graphite API.
For simplicity, a snippet of the data sent by Graphite is:
[
{
"target": "server1.loadavg.1min",
"datapoints": [
[
0.16,
1422770850
],
[
0.16,
1422770880
],
[
null,
1422771120
]
]
},
{
"target": "server2.loadavg.1min",
"datapoints": [
[
0.19,
1422770850
],
[
null,
1422771390
],
[
0.14,
1422771420
]
]
}
]
I've been reading through the go json tutorial about how to use a generic interface{} for JSON arbitrary data, but I'm struggling with some aspects of the process.
I've tried to define a structure that will hold this data, read the file contents and unmarshal it to this structure:
type Graphite struct {
Metric struct {
Target string `json:"target"`
Datapoints [][]float64 `json:"datapoints"`
}
}
var results []Graphite
err = json.Unmarshal(d, &r)
if err != nil {
panic(err)
}
fmt.Printf("%v
", r)
But the result is:
[{{ []}} {{ []}}]
I could do it of course with a generic interface{} but I would like to know what am I missing here.
Could you please help me?
Thank you!