Nested structure can not be encode correctly

I’m doing a nested structure and try to encode it to a json string. But only the first struct is sending in the string no child is encoded to the string. Tried to print out by data.children[0], data.children[0].chilldren[0], the data seems correctly saved. What’s the problem?

type Data struct {
ID int json:"ID"
Name string json:"Name"
children []Data json:"children"

type DbData struct{
ID int mapstructure:"ID" json:"ID" gorm:"primary_key;column:ID"
Name string mapstructure:"Name" json:"Name" gorm:"primary_key;column:Name"
ParentID int mapstructure:"ParentID" json:"ParentID" gorm:"primary_key;column:ParentID"

func getChildData(Id int) Data{
var data Data
var count = 0
var dbData DbData
childdata := make([]DbData,0)
db.Where(“ID = ?”,Id).First(&dbData)
db.Where(“ParentID = ?”,Id).Find(&childdata).Count(&Count)
data.ID = dbData.ID
data.Name = dbData.Name
for i := 0; i < count; i++ {
data .children = append(data .children, getChildData(childdata [i].ID))
if count == 0 {
data.children = make([]Data, 0)


Due to the limits of reflection, all fields you want to JSONify needs to be exposed, aka start with an uppercase letter.


Thanks,. just not noticed that, it’s working.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.