I need to create JSON file to reflect tree structure in Python / Django.
I have two options - call database on creating each node of tree or extract data from database all at once and create tree afterwards. I think calling database on each node for thousands of data would be a slow task, so decided to create raw JSON data from database at first and then next create JSON tree. I created raw JSON data from database [PostgreSQL]. The raw JSON data looks like:
[{"Name": "Michael Andrew", "class": "A", "order": "B", "family": "C"}, {"Name": "John Belew", "class": "A", "order": "D", "family": "E"}, {"Name": "Warren Noah", "class": "F", "order": "G", "family": "H"}]
I want following JSON tree to be made from the data above in Django / Python:
{
"name": "jsontree",
"children":[
{
"name": "A",
"children": [
{
"name": "B",
"children": [
{
"name": "C",
"children": [
{
"name": "Michael Andrew"
}]
}]
},
{
"name": "D",
"children": [
{
"name": "E",
"children": [
{
"name": "John Belew"
}]
}]
}]
},
{
"name": "F",
"children": [
{
"name": "G",
"children": [
{
"name": "H",
"children": [
{
"name": "Warren Noah"
}]
}]
}]
}]
}
What may be the efficient way to do so in Python? I heard of django-mptt to create JSON tree from database but have no idea about what it actually is.