Chetan Kumar
Chetan Kumar

Reputation: 328

how to build dynamic structure in golang?

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

Upvotes: 0

Views: 2683

Answers (2)

Darigaaz
Darigaaz

Reputation: 1500

It's better to define your desired structure first (with nested structures as levels). And then implement UnmarshalJSON([]byte) error interface to convert incoming data do desired structure.

I do not want to write long examples unless I am sure what do you want =) I would like to take similar approach as I used in Merge a dynamic data structure in Go.

Do you need those "A1".."ZN", ("a1".."an")...("z1".."zn") keys? Or slices will be enough? Like:

type S struct {
    ID bson.ObjectId
    Data [][][]string
}

Or may be convert inner []string

[
   "abc",
   "def",
   "ghi"
]

to some struct, if there are only 3 elements?

Upvotes: 0

David Budworth
David Budworth

Reputation: 11626

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)

Upvotes: 3

Related Questions