Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

sqlite - How can I create a hierarchical json response in FLASK

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

question from:https://stackoverflow.com/questions/65854960/how-can-i-create-a-hierarchical-json-response-in-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
                                }
                             ]
                         }
                      ]
                  }
              ]
          }
      ]
   }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...