0

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

Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
Chetan Kumar
  • 328
  • 1
  • 4
  • 18
  • Related / possible duplicate of [Taking a JSON string, unmarshaling it into a map, editing, and marshaling it into a byte slice seems more complicated then it should be](http://stackoverflow.com/questions/28877512/taking-a-json-string-unmarshaling-it-into-a-mapstringinterface-editing-an) – icza Jun 06 '16 at 16:25

2 Answers2

3

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)

David Budworth
  • 11,248
  • 1
  • 36
  • 45
0

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?

Community
  • 1
  • 1
Darigaaz
  • 1,414
  • 10
  • 11