I have below JSON
[{
"id": 8,
"title": "Indonesia",
"type": "country",
"attributes": {
"regionCode": "ID",
"information": {
"title": "Welcome to Indonesia",
"content": "We only serve selected areas in Indonesia.",
"image": "indo.png"
}
},
"children": [
{
"id": 9,
"title": "Jakarta",
"type": "city",
"attributes": {
"regionCode": "ID-JKT",
"information": {
"title": "Welcome to Capital City of Indonesia",
"content": "We only serve selected areas in Jabotabek",
"image": "jakarta.png"
}
}
},
{
"id": 10,
"title": "Bali",
"type": "city",
"attributes": {
"regionCode": "ID-BAL",
"information": {
"title": "Welcome to the beach city Bali",
"content": "We only serve selected areas in Bali.",
"image": "bali.png"
}
}
}
]
}]
Technically this is a nested structure to itself as the children node, and the children might have children as well. But i just couldnt get it right when im using unmarshal to decode this.
The working one is like below i have no problem defining the attribute as a separate struct so assuming i have locationAttribute struct
type locationNode []struct {
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Attributes locationAttribute `json:"attributes"`
Children []struct {
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Attributes locationAttribute `json:"attributes"`
Children []interface{} `json:"children"`
} `json:"children"`
}
However im expecting to be something like below
type locationNode []struct {
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Attributes locationAttribute `json:"attributes"`
Children []locationNode `json:"children"`
}
any help will be much appreciated
Okay I managed to resolve this - it's actually a silly careless mistake, since my struct is already an array I should NOT define the children as an array.
So below definition works
type locationNode []struct {
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Attributes locationAttribute `json:"attributes"`
Children locationNode `json:"children"`
}
Using https://mholt.github.io/json-to-go/ your JSON converts to
type AutoGenerated struct {
ID int `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Attributes struct {
RegionCode string `json:"regionCode"`
Information struct {
Title string `json:"title"`
Content string `json:"content"`
Image string `json:"image"`
} `json:"information"`
} `json:"attributes"`
Children []struct {
ID int `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Attributes struct {
RegionCode string `json:"regionCode"`
Information struct {
Title string `json:"title"`
Content string `json:"content"`
Image string `json:"image"`
} `json:"information"`
} `json:"attributes"`
} `json:"children"`
}
Besides the fact that in the JSON text the "id"
field is a number and not string
, it works for me.
Omitting the "attributes"
to make the example shorter:
type locationNode struct {
ID int `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Children []locationNode `json:"children"`
}
func main() {
ln := locationNode{}
err := json.Unmarshal([]byte(src), &ln)
fmt.Printf("%+v %v
", ln, err)
}
const src = `{
"id": 8,
"title": "Indonesia",
"type": "country",
"attributes": {
"regionCode": "ID",
"information": {
"title": "Welcome to Indonesia",
"content": "We only serve selected areas in Indonesia.",
"image": "indo.png"
}
},
"children": [
{
"id": 9,
"title": "Jakarta",
"type": "city",
"attributes": {
"regionCode": "ID-JKT",
"information": {
"title": "Welcome to Capital City of Indonesia",
"content": "We only serve selected areas in Jabotabek",
"image": "jakarta.png"
}
}
},
{
"id": 10,
"title": "Bali",
"type": "city",
"attributes": {
"regionCode": "ID-BAL",
"information": {
"title": "Welcome to the beach city Bali",
"content": "We only serve selected areas in Bali.",
"image": "bali.png"
}
}
}
]
}`
Output (try it on the Go Playground):
{ID:8 Title:Indonesia Type:country Children:[{ID:9 Title:Jakarta Type:city Children:[]} {ID:10 Title:Bali Type:city Children:[]}]} <nil>
It certainly looks like defining Children
in the way you expect works fine. The following both marshals and unmarshals fine.
type Bob struct {
ID string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Attributes int `json:"attributes"`
Children []Bob `json:"children"`
}