0

I have a single table in database like database table. I want to search a child from database and return a hierarchical JSON to a front end in order to create a tree. How can I do that in FLASK. My expected JSON for mat should be like expected JSON

2
  • Could you please add your expected JSON format with the table data?
    – avijit
    Jan 23, 2021 at 6:44
  • @avijitbhattacharjee, I have added the JSON format in the question.
    – abelfit
    Jan 23, 2021 at 9:09

1 Answer 1

0

Since you have tagged your question with , this post assumes you are using Python as well. To format your database values in JSON string, you can query the db and then use recursion:

import sqlite3, collections
d = list(sqlite3.connect('file.db').cursor().execute("select * from values"))
def get_tree(vals):
  _d = collections.defaultdict(list)
  for a, *b in vals:
    _d[a].append(b)
  return [{'name':a, **({} if not (c:=list(filter(None, b))) else {'children':get_tree(b)})} for a, b in _d.items()]

import json
print(json.dumps(get_tree(d), indent=4))

Output:

[
  {
    "name": "AA",
    "children": [
        {
            "name": "BB",
            "children": [
                {
                    "name": "EE",
                    "children": [
                        {
                            "name": "JJ",
                            "children": [
                                {
                                    "name": "EEV"
                                },
                                {
                                    "name": "FFW"
                                }
                            ]
                        },
                        {
                            "name": "KK",
                            "children": [
                                {
                                    "name": "HHX"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "name": "CC",
            "children": [
                {
                    "name": "FF",
                    "children": [
                        {
                            "name": "LL",
                            "children": [
                                {
                                    "name": "QQY"
                                }
                            ]
                        },
                        {
                            "name": "MM",
                            "children": [
                                {
                                    "name": "RRV"
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "GG",
                    "children": [
                        {
                            "name": "NN",
                            "children": [
                                {
                                    "name": "SSW"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "name": "DD",
            "children": [
                {
                    "name": "HH",
                    "children": [
                        {
                            "name": "OO",
                            "children": [
                                {
                                    "name": "TTZ"
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "II",
                    "children": [
                        {
                            "name": "PP",
                            "children": [
                                {
                                    "name": "UUW"
                                }
                             ]
                         }
                      ]
                  }
              ]
          }
      ]
   }
]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.