I got a problem with parsing JSON in golang where I receive response from API in JSON format which nests same form of JSON in multiple levels.
Response from API is following
{
"podKategoria": {
"podKategoriaTyp": "area",
"nazwaWyswietlana": "Area",
"podKategorie": [
{
"podKategoriaTyp": "somethingelse",
"nazwaWyswietlana": "Display something else",
"podKategoria": {
"podKategoriaTyp": "and other thing",
"nazwaWyswietlana": "Display and other thing",
"podKategorie": [
{
"podKategoriaTyp": "sub nd other thing",
"nazwaWyswietlana": "display nd other thing"
},
{
"podKategoriaTyp": "sub 2 nd other thing",
"nazwaWyswietlana": "display sub 2 nd other thing"
},
{
"podKategoriaTyp": "sub 3 nd other thing",
"nazwaWyswietlana": "display sub 3 nd other thing"
}
]
}
},
{
"podKategoriaTyp": "another one",
"nazwaWyswietlana": "display another one",
"podKategoria": {
"podKategoriaTyp": "and and another one ",
"nazwaWyswietlana": "display it another one",
"podKategorie": [
{
"podKategoriaTyp": "sub 1 another one",
"nazwaWyswietlana": "display sub 1"
},
{
"podKategoriaTyp": "sub 2 another one",
"nazwaWyswietlana": "display sub 2"
},
]
}
},
]
}
}
Following this I have used https://mholt.github.io/json-to-go/ to try and create structure for me. The most appropriate approach seemed to be creating base structure like
type Struktury struct {
PodKategorie PodKategoria `json:"podKategoria"`
}
type PodKategoria struct {
PodKategoriaTyp string `json:"podKategoriaTyp"`
NazwaWyswietlana string `json:"nazwaWyswietlana"`
PodKategorie []struct {
SubCategoryType string `json:"podKategoriaTyp"`
DisplayName string `json:"nazwaWyswietlana"`
} `json:"podKategorie, omitempty"`
}
But as you can see JSON structure is a bit tricky and I'm kinda stuck trying to figure the best way to proper unmarshliing this JSON string :/
Initial attempt to just use defaults allows me to get root of items ( tested in playground https://play.golang.org/p/uQMnMGEtXD- ) therefore this brings me to idea that maybe custom unmarshall interface implemention is the way to go here ? Any pointers how to tackle this would be appreciated