search by tags

for the user

adventures into the land of the command line

unmarshalling nested json in go

so.. when looking for examples on the internets for how to unmarshall json into maps in golang, the examples i came across were always super simple, like this one and this one

therefore.. example of unmarshalling nested json of multiple types into some Objects

package main

import "fmt"
import "encoding/json"

type Targets struct {
    DiscoveredLabels map[string]string
    Labels map[string]string
    ScrapeUrl string
    LastError string
    LastScrape string
    Health string
}

type Something struct {
    Status string
    Data struct {
        ActiveTargets []Targets
    }
}

func main() {

    data := []byte(`{
        "status": "success",
        "data": {
            "activeTargets": [{
                "discoveredLabels": {
                    "__address__": "10.123.0.1:1234",
                    "__meta_kubernetes_endpoint_port_name": "http",
                    "__meta_kubernetes_endpoint_port_protocol": "TCP",
                    "__meta_kubernetes_endpoint_ready": "true",
                    "__meta_kubernetes_endpoints_name": "blackbox-prometheus-blackbox-exporter",
                    "__meta_kubernetes_namespace": "monitoring",
                    "__meta_kubernetes_pod_container_name": "blackbox-exporter",
                    "__meta_kubernetes_pod_container_port_name": "http",
                    "__meta_kubernetes_pod_container_port_number": "1234",
                    "__meta_kubernetes_pod_container_port_protocol": "TCP",
                    "__meta_kubernetes_pod_host_ip": "10.123.0.4",
                    "__meta_kubernetes_pod_ip": "10.123.0.1",
                    "__meta_kubernetes_pod_label_app": "prometheus-blackbox-exporter",
                    "__meta_kubernetes_pod_label_pod_template_hash": "3277369166",
                    "__meta_kubernetes_pod_label_release": "blackbox",
                    "__meta_kubernetes_pod_name": "blackbox-prometheus-blackbox-exporter-76cc7pxgbc",
                    "__meta_kubernetes_pod_node_name": "node-0",
                    "__meta_kubernetes_pod_ready": "true",
                    "__meta_kubernetes_pod_uid": "bc09575d-23c2-11e9-8585-b2a01ed96f93",
                    "__meta_kubernetes_service_label_app": "prometheus-blackbox-exporter",
                    "__meta_kubernetes_service_label_chart": "prometheus-blackbox-exporter-0.1.1",
                    "__meta_kubernetes_service_label_heritage": "Tiller",
                    "__meta_kubernetes_service_label_release": "blackbox",
                    "__meta_kubernetes_service_name": "blackbox-prometheus-blackbox-exporter",
                    "__metrics_path__": "/probe",
                    "__param_module": "http_2xx",
                    "__param_target": "https://www.example.com",
                    "__scheme__": "http",
                    "job": "monitoring/blackbox-exporter/35"
                },
                "labels": {
                    "endpoint": "1234",
                    "instance": "10.123.0.2:1234",
                    "job": "blackbox-prometheus-blackbox-exporter",
                    "namespace": "monitoring",
                    "pod": "blackbox-prometheus-blackbox-exporter-76cc7pxgbc",
                    "service": "blackbox-prometheus-blackbox-exporter"
                },
                "scrapeUrl": "www.example.com",
                "lastError": "",
                "lastScrape": "2019-01-31T15:57:36.094393512Z",
                "health": "up"
            }, {
                "discoveredLabels": {
                    "__address__": "10.123.0.2:1234",
                    "__meta_kubernetes_endpoint_port_name": "http",
                    "__meta_kubernetes_endpoint_port_protocol": "TCP",
                    "__meta_kubernetes_endpoint_ready": "true",
                    "__meta_kubernetes_endpoints_name": "blackbox-prometheus-blackbox-exporter",
                    "__meta_kubernetes_namespace": "monitoring",
                    "__meta_kubernetes_pod_container_name": "blackbox-exporter",
                    "__meta_kubernetes_pod_container_port_name": "http",
                    "__meta_kubernetes_pod_container_port_number": "1234",
                    "__meta_kubernetes_pod_container_port_protocol": "TCP",
                    "__meta_kubernetes_pod_host_ip": "10.123.0.4",
                    "__meta_kubernetes_pod_ip": "10.123.0.2",
                    "__meta_kubernetes_pod_label_app": "prometheus-blackbox-exporter",
                    "__meta_kubernetes_pod_label_pod_template_hash": "3277369166",
                    "__meta_kubernetes_pod_label_release": "blackbox",
                    "__meta_kubernetes_pod_name": "blackbox-prometheus-blackbox-exporter-76cc7pxgbc",
                    "__meta_kubernetes_pod_node_name": "node-0",
                    "__meta_kubernetes_pod_ready": "true",
                    "__meta_kubernetes_pod_uid": "bc09575d-23c2-11e9-8585-b2a01ed96f93",
                    "__meta_kubernetes_service_label_app": "prometheus-blackbox-exporter",
                    "__meta_kubernetes_service_label_chart": "prometheus-blackbox-exporter-0.1.1",
                    "__meta_kubernetes_service_label_heritage": "Tiller",
                    "__meta_kubernetes_service_label_release": "blackbox",
                    "__meta_kubernetes_service_name": "blackbox-prometheus-blackbox-exporter",
                    "__metrics_path__": "/probe",
                    "__param_module": "http_2xx",
                    "__param_target": "https://www.anotherexample.com",
                    "__scheme__": "http",
                    "job": "monitoring/blackbox-exporter/23"
                },
                "labels": {
                    "endpoint": "1234",
                    "instance": "10.123.0.2:1234",
                    "job": "blackbox-prometheus-blackbox-exporter",
                    "namespace": "monitoring",
                    "pod": "blackbox-prometheus-blackbox-exporter-76cc7pxgbc",
                    "service": "blackbox-prometheus-blackbox-exporter"
                },
                "scrapeUrl": "www.anotherexample.com",
                "lastError": "",
                "lastScrape": "2019-01-31T15:58:03.617583017Z",
                "health": "up"
            }]
        }
    }`)


    something := Something{}

    err := json.Unmarshal(data, &something)
    if err != nil {
        fmt.Println("error: ", err)
    }

    fmt.Println(something.Status)
    fmt.Println(something.Data)
    fmt.Println(something.Data.ActiveTargets)
    for _, each := range something.Data.ActiveTargets {

        fmt.Println(each.DiscoveredLabels)
        fmt.Println(each.Labels)
        fmt.Println(each.ScrapeUrl)
        fmt.Println(each.LastError)
        fmt.Println(each.LastScrape)
        fmt.Println(each.Health)

    }

}