I can only send ONE parameter into a Go template. But I have two (or more JSON) maps that I want to join and add a “header” into
The final outcome should be this. I want to add “main” and “sub” before each JSON.
const jsondata = `{
"main": [{ <---------add main header
"menu_id": "1",
"menu_txt": "Home"
}, {
"menu_id": "2",
"menu_txt": "Prefs"
}],
"sub": [{ <---------add sub header
"menu_id": "3",
"menu_txt": "Test"
}, {
"menu_id": "4",
"menu_txt": "Test2"
}]
}
`
The two JSON objects (maps?) are loaded separately and I want them to be joined.
func load_main(main string) []map[string]interface{} {
resp, err := http.Get("https://api3.go4webdev.org/menu/" + main)
if err != nil {
fmt.Println("No response")
}
defer resp.Body.Close()
menu, err := io.ReadAll(resp.Body)
var data []map[string]interface{}
if err := json.Unmarshal([]byte(menu), &data); err != nil {
panic(err)
}
return data
}
func load_sub(sub string) []map[string]interface{} {
resp, err := http.Get("https://api3.go4webdev.org/menu/" + sub)
if err != nil {
fmt.Println("No response")
}
defer resp.Body.Close()
menu, err := io.ReadAll(resp.Body)
var data []map[string]interface{}
if err := json.Unmarshal([]byte(menu), &data); err != nil {
panic(err)
}
return data
}
The output of each load is something like
[map[menu_id:home menu_txt:Home…] (main)
[map[menu_id:code menu_txt:Code…] (sub)
How do I add headers into each map and join the two JSON into ONE single JSON map?