1

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.

2
  • Query and then json.dumps more info on the official documentation (regarding json you can find it on python documentation) Commented Jun 5, 2014 at 19:11
  • Welcome to Stack Overflow! It's always important to tell people what you have tried, including snippets of any failed attempts so that they can understand what avenues you have missed. It's important because it motivates people to answer and it's important because it makes it easier to give high quality, relevant answers. With the current state of the question, this hasn't been achieved. If you edit the question, it's possible that the question can be prevented from being closed and the quantity, quality and clarity of answers you get will improve as well. Commented Jun 8, 2014 at 20:12

1 Answer 1

1

To dump a json file from the database use python manage.py dumpdata. Use the --indent=2 to get the output as a json tree. If you had a django app called blog then run the following command:

python manage.py dumpdata blog --indent=2 > blog/blog.json

or for the django User model:

python manage.py dumpdata auth.User --indent=2 > user.json
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.