2

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, &timeseries); 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.

Jorge
  • 21
  • 1
  • 3
  • 5
    Use a map when the keys are not known: `DataPoints map[string][]float64`. Possible duplicate of [How to parse/deserlize a dynamic JSON in Golang](https://stackoverflow.com/questions/29347092/how-to-parse-deserlize-a-dynamic-json-in-golang) and others. – Charlie Tumahai Jul 19 '19 at 02:36
  • 2
    Possible duplicate of [How to parse/deserialize dynamic JSON in Golang](https://stackoverflow.com/questions/29347092/how-to-parse-deserialize-dynamic-json-in-golang) – hewiefreeman Jul 19 '19 at 03:25
  • Thank you! Maybe this os my solution! – Jorge Jul 20 '19 at 16:46

2 Answers2

1

I think unmarshal to map may be ok... then for range map

bytes := []byte("a")
var a = make(map[string]string)
err := json.Unmarshal(bytes, &a)
if err != nil {
    panic(err)
}
art of go
  • 21
  • 2
-1

So i would suggest using something like this - https://stackoverflow.com/a/21363587/5627808

To get a slice of keys. Then just iterate over them selecting the data in the map.

Hayhay G
  • 24
  • 4