Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Query and then json.dumps more info on the official documentation (regarding json you can find it on python documentation) –  petkostas Jun 5 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. –  Veedrac Jun 8 at 20:12
add comment

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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