Actually there is JSON, you need to convert it into a tree. And get access to the nodes. Tried it like this. Immediately I’ll clarify that the JSON structure can change (some fields can be added or deleted, but the main ones are always the same)

package main import ( "encoding/json" //"fmt" ) //type nestedMaps map[string]nestedMaps func main() { byt := []byte(`{ "node1": { "value": "1", "node2": { "value": "2", "node4": { "value": "4" } }, "node3": { "value": "3" } } }`) var dat map[string]interface{} if err := json.Unmarshal(byt, &dat); err != nil { panic(err) } //fmt.Println(dat["node1"]["node2"]["node4"]["value"]); } 

    1 answer 1

    You cannot take a value from interface{} without a type approval . So you have to do something like

     dat["node1"].(map[string]interface{})["node2"].(map[string]interface{})["node4"].(map[string]interface{})["value"] 

    Playground: https://play.golang.org/p/oewoOonOXD .

    And if inside is not the type, it will panic. So if you do not want to panic (and you should not panic), then you will have to use a "safe type statement"

     node1, ok := dat["node1"].(map[string]interface{}) if !ok { return fmt.Errorf("bad type of node1: %T", dat["node1"]) } // ...