Golang with inner struct with data or null

I got this code,

type employee struct {          
    Id              string        `json:"id"`  
    Name              string        `json:"name"`  	
    Skills            struct {
        Id         int    `json:"id"`
        Name         int    `json:"name"`
    } `json:"desc"`
}

var bearer = xxxxxxxx
tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}  
req, _ := http.NewRequest("GET", url, nil)    
req.Header.Set("Authorization", fmt.Sprintf("%v", bearer))  
req.Close = false
res, err := tr.RoundTrip(req)
if err != nil {
	log.Println(err)
	return "error"
}
body, e := io.ReadAll(res.Body)  
if e != nil {
	log.Println(e)
	return "error"
}
var t skills
ers := json.Unmarshal([]byte(string(body)), &t) 
if ers != nil {
	log.Println(ers)
}

With data example below that got no error,

{
 "Id": "01",
 "Name": "John",
 "Skills": {
    "Id": "01",
	"Name": "Developer"
 } 
}

But another data example below will have error because of the null on the Skills,

{
 "Id": "01",
 "Name": "John",
 "Skills": null
}

Here’s the error log,

2024/05/01 18:58:29 http: panic serving 10.x.x.x:60426: runtime error: index out of range [2] with length 1
goroutine 6 [running]:
net/http.(*conn).serve.func1()
	/usr/lib/golang/src/net/http/server.go:1854 +0xbf
panic({0x797720, 0xc0000c40d8})
	/usr/lib/golang/src/runtime/panic.go:890 +0x263
main.endpointHandler({0xc00008d998?, 0xe?}, 0xc00014a000, {0xe?, 0x7b9dc2?, 0xc00008d9e8?})
	/builds/devops/pipeline/gitops-notification/src_repo/cmd/webhookrl/webhookrl.go:225 +0xb08
main.rateLimiter.func1({0x84af80, 0xc000150000}, 0xc00014a000, {0xc00008da80?, 0xc00008db70?, 0x6c7c25?})
	/builds/devops/pipeline/gitops-notification/src_repo/cmd/webhookrl/webhookrl.go:263 +0xb6
github.com/julienschmidt/httprouter.(*Router).ServeHTTP(0xc000132000, {0x84af80, 0xc000150000}, 0xc00014a000)
	/builds/devops/pipeline/gitops-notification/src_repo/vendor/github.com/julienschmidt/httprouter/router.go:474 +0x1f7
net/http.serverHandler.ServeHTTP({0x849f30?}, {0x84af80, 0xc000150000}, 0xc00014a000)
	/usr/lib/golang/src/net/http/server.go:2936 +0x316
net/http.(*conn).serve(0xc000146000, {0x84b1a8, 0xc00007cde0})
	/usr/lib/golang/src/net/http/server.go:1995 +0x612
created by net/http.(*Server).Serve
	/usr/lib/golang/src/net/http/server.go:3089 +0x5ed
.
.
.

If I change json struct like this,

type skills struct {          
    Id              string        `json:"id"`  
    Name              string        `json:"name"`  	
    Skills            []interface{} `json:"skills"`     
}

I will have this error instead:

json: cannot unmarshal object into Go struct field employee.skills of type []interface {}
http: panic serving 10.x.x.x:45812: runtime error: index out of range [2] with length 1
goroutine 18 [running]:

I’m not sure this is the right one I need to put,

             []interface{}

That’s only working on my other code if it array not another struct.

Thanks!

I checked my previous json, this is what I have,

type employee struct {          
    Id              string        `json:"id"`  
    Name              string        `json:"name"`  	
    Skills            struct {
        Id         int    `json:"id"`
        Name         int    `json:"name"`
    } `json:"desc"`
} `json:"unknown,omitempty"`

Hmm, I don’t understand why you have json:"desc" in the Skills field and what is var t skills doing there, but I will assume that it’s a typo. But, I think the root cause of your error doesn’t have anything to do with your json marshal or struct definition. It might be somewhere else in your code.