I'm consuming a JSON from an API, and one of the return fields is keyed with a dynamic value, such as SERVICE-1234, from the return below.
{
"timeseriesId": "serversidefailurerate",
"displayName": "Number of server side errors",
"dimensions": [
"SERVICE"
],
"unit": "Percent (%)",
"detailedSource": "Services",
"types": [],
"dataResult": {
"dataPoints": {
"SERVICE-1234": [
[
1563472440000,
0.8034610630407911
]
]
},
"unit": "Percent (%)",
"resolutionInMillisUTC": 3600000,
"aggregationType": "AVG",
"entities": {
"SERVICE-1234": "server"
},
"timeseriesId": "serversidefailurerate"
},
"aggregationTypes": [
"AVG",
"SUM",
"MIN",
"MAX"
],
"filter": "BUILTIN"
}
I'm using the following code to collect and extract the values, but I do not know how to reference the dynamic keys in the struct.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Timeseries struct {
TimeseriesId string
DisplayName string
Dimensions []string
Unit string
DetailedSource string
Types []string
DataResult struct {
DataPoints interface{}
ResolutionInMillisUTC int
AggregationType string
Entities interface{}
TimeseriesId string
}
AggregationTypes []string
Filter string
}
func main() {
response, err := http.Get("url")
if err != nil {
fmt.Printf("The HTTP request failed with error %s\n", err)
} else {
temp, _ := ioutil.ReadAll(response.Body)
// fmt.Println(string(temp))
var timeseries Timeseries
if err := json.Unmarshal(temp, ×eries); err != nil {
fmt.Println("There was an error:", err)
}
fmt.Println(timeseries)
}
}
I am expect the output of values in the respective keys, but i don't know how to start to do.