I have different json byte inputs that I need to unmarshal into a nested json structure. I am able to unmarshal the json into the struct App. However I am unable to add to "status" struct.
I tried to unmarshal, but that doesnt work since my app1 & app2 are of type App instead of bytes. And trying to set directly gives the error "cannot use app1 (type App) as type []App in assignment"
package main import ( "encoding/json" "fmt" "reflect" ) type App struct { Appname string `json:"appname"` Builds string `json:"builds"` } type Status struct { Apps []App `json:"apps"` } func main() { jsonData1 := []byte(` { "appname": "php1", "buildconfigs":"deleted" } `) jsonData2 := []byte(` { "appname": "php2", "buildconfigs":"exists" } `) // unmarshal json data to App var app1 App json.Unmarshal(jsonData1, &app1) var app2 App json.Unmarshal(jsonData2, &app2) var status Status //json.Unmarshal(app1, &status) //json.Unmarshal(app2, &status) status.Apps = app1 status.Apps = app2 fmt.Println(reflect.TypeOf(app1)) fmt.Println(reflect.TypeOf(app1)) }