I am new to go and I understand how to marshall json data into custom predefined Struct types but I'm currently working with a JSON set that can have dynamic keys & values with every call. I can marshall this dynamic data into a map/interface no problem but I'm a little lost with how to access the item that are deeply nested in an array.
Here is sample JSON that I'm working with from the USDOL website
{
"name": "osha-establishment",
"count": 15,
"frequency": "Manual Crawl",
"version": 4,
"newdata": true,
"lastrunstatus": "success",
"lastsuccess": "Mon Dec 08 2014 11:19:57 GMT+0000 (UTC)",
"thisversionstatus": "success",
"results": {
"est_search": [
{
"est_name": "Chipotle Mexican Grill",
"state": "MN",
"activity": {
"href": "https://www.osha.gov/pls/imis/establishment.inspection_detail?id=317911832",
"text": "317911832"
},
"opened": "11/20/2014",
"rid": "0552700",
"type": "Complaint",
"sc": "Partial",
"sic": {
"href": "https://www.osha.gov/pls/imis/sic_manual.display?id=44&tab=description",
"text": "5812"
},
"naics": "722110",
"vio": ""
},
{
"est_name": "Chipotle Mexican Grill, Inc.",
"state": "AZ",
"activity": {
"href": "https://www.osha.gov/pls/imis/establishment.inspection_detail?id=317801066",
"text": "317801066"
},
"opened": "07/14/2014",
"rid": "0950411",
"type": "Complaint",
"sc": "Partial",
"sic": {
"href": "https://www.osha.gov/pls/imis/sic_manual.display?id=44&tab=description",
"text": "5812"
},
"naics": "722211",
"vio": "2"
}
]
}
}
I'd like to access the values of the sic
keys in this JSON set.
Here is my broke go code:
package main
import (
"encoding/json"
"log"
"net/http"
)
func main() {
resp, err := http.Get("https://www.kimonolabs.com/api/2oowt0xq?apikey=")
if err != nil {
// handle error
}
defer resp.Body.Close()
var result map[string]interface{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&result); err != nil {
// handle error
log.Println("This is an error")
}
log.Println(result["results"].(map[string]interface{})["est_search"], err)
}
Any help showing me how to access the sic
values would be greatly appreciated.