I have this json file,In this json file there are n number of key as we see A1,B1......................................................................zn, a1,a2.......................................................................an, b1..........................................................................bn etc.
{
"_id": "5746992a54c1ae24d53ce651",
"A1": [
{
"a1": [
"abc",
"def",
"ghi"
]
},
{
"a2": [
"abc",
"def",
"ghi"
]
},
.
.
,
{
"an": [
"abc",
"def",
"ghi"
]
}
],
"B1": [
{
"b1": [
"abc",
"def",
"ghi"
]
},
{
"b2": [
"abc",
"def",
"ghi"
]
},
{
"bn": [
"abc",
"def",
"ghi"
]
}
],
.
.
.
,
"Bn": [
{
"b1": [
"abc",
"def",
"ghi"
]
},
{
"b2": [
"abc",
"def",
"ghi"
]
},
{
"bn": [
"abc",
"def",
"ghi"
]
}
]
}
how to call their structure in golang
type Level1 struct {
TAGID bson.ObjectId `json:"_id" bson:"_id"`
LEVELTAG2 []Level2 `json:"level2" bson:"level2"`
}
type LevelTag2 struct{
LEVEL3 []string `json:"level3" bson:"level3"`
}
I build this structure in golang is there right way or any other way please help me out
When the keys are unknown at compile time, you really can only use map[string]interface{}
and then some helper functions to navigate that structure.
If the keys literally are a1, a2, a3 etc... you can make an actual struct, but it would not be pretty as you have to spell out each and every key.
In general, when the keys of your documents are part of the data, you can't really create static structures.
And by "part of the data" I mean:
{
"billy":23,
"tommy":24
}
vs
[
{"name":"billy", "age":23},
{"name":"tommy", "age":24}
]
This second form can be represented as: struct { Name string, Age int }
while the first one can really only be: map[string]int or map[string]interface{} (if the structure is deep)