Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a infinite nested json and access the same in go lang data structures?

How to create a infinite nested json and access the same in go lang data structures,

For example below is the sample json with 3 levels, in general it should be dynamic, user can add any children by selecting the value from the dropdown which is populated tree like dropdown on UI.

{
        "value": {
            "Id": "1",
            "Text": "abcd",
            "Parent": ""
        },
        "children": [
            {
                "value": {
                    "Id": "2",
                    "Text": "abcd",
                    "Parent": "1"
                },
                "children": [
                    {
                        "value": {
                            "Id": "3",
                            "Text": "abcd",
                            "Parent": "1"
                        }
                    }
                ]
            }
        ]
    }

structures in go: I have created this go data structure but it will access only upto 3 levels based on the above json, can we make this data structure as dynamic where it should handle infinite nested json.

 type AutoGenerated struct {
        Value struct {
            ID     string `json:"Id"`
            Text   string `json:"Text"`
            Parent string `json:"Parent"`
        } `json:"value"`
        Children []struct {
            Value struct {
                ID     string `json:"Id"`
                Text   string `json:"Text"`
                Parent string `json:"Parent"`
            } `json:"value"`
            Children []struct {
                Value struct {
                    ID     string `json:"Id"`
                    Text   string `json:"Text"`
                    Parent string `json:"Parent"`
                } `json:"value"`
            } `json:"children"`
        } `json:"children"`
    }

Note: we can add n number of parents and n number of children, is this possible in Go data structures?

can you suggest best and easy way to implement this?

How can we add/delete/edit any parent or child? (The above json example will come from UI) ? To add/delete/edit any parent or child which json structure or id needed?

like image 519
btc user Avatar asked Oct 26 '25 14:10

btc user


1 Answers

You can use recursive structures in Go to represent this json (by recursive, I mean that Level contains a []Level):

type Value struct {
    ID     string
    Text   string
    Parent string
}

type Level struct {
    Value    Value `json:"value"`
    Children []Level
}

Given the json you listed as the string j, I can now unmarshal it as follows:

var root Level
err := json.Unmarshal([]byte(j), &root)
if err != nil {
    panic(err)
}
fmt.Println(root)
fmt.Println(root.Children[0])
fmt.Println(root.Children[0].Children[0])

This outputs:

{{1 abcd } [{{2 abcd 1} [{{3 abcd 1} []}]}]}
{{2 abcd 1} [{{3 abcd 1} []}]}
{{3 abcd 1} []}

Go playground link

like image 131
Marc Avatar answered Oct 29 '25 03:10

Marc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!