N
N
nurzhannogerbek2019-03-09 21:27:34
Oracle
nurzhannogerbek, 2019-03-09 21:27:34

How to parse hierarchical data in Golang?

Hello comrades! Please help me to solve the problem. Oracle
database has a table with the following structure:

| organization_id | ogranization_name | parent_id | level |
|-----------------|-------------------|-----------|-------|
| 1               | Facebook          |           | 0     |
| 2               | Instagram         | 1         | 1     |
| 3               | Whatsapp          | 1         | 1     |
| 4               | Dynamic           | 2         | 2     |
| 5               | Google            |           | 0     |

This table shows the structure of storing hierarchical data. In my case, this is a hierarchy of organizations.
I have a question, how do you normally parse data in a Golang application to get a JSON tree structure as a response:
[
    {
        "organization_id": 1
        "ogranization_name": "Facebook",
        "childs": [
            {
                "organization_id": 2,
                "ogranization_name": "Instagram",
                "childs": null
            },
            {
                "organization_id": 3,
                "ogranization_name": "Whatsapp",
                "childs": [
                    {
                        "organization_id": 4,
                        "ogranization_name": "Dynamic",
                        "childs": null
                    }
                ]
            }
        ]
    },
    {
        "organization_id": 5
        "ogranization_name": "Google",
        "childs": null
    }
]

Are there packages for this purpose? Are there any best practices that you know about and have applied?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2019-03-09
@deliro

Ready packages? Yes, encoding/json. Hierarchical structures are the same as regular ones.
https://play.golang.org/p/SUE3lLBciMQ
PS The JSON you provided is invalid. There are two commas missing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question