0

The levels needs to look like this:

  • Task: xxxxxx1
    • Subtask: xxxxxx2
      • Sub subtask: xxxxxx3
      • Sub subtask: xxxxxx4
  • Task: xxxxxx5
    • Subtask: xxxxxx6

I am struggling to arrange all tasks and subtasks in a new dict at the correct level like the example above with the example data. Please keep in mind, the example shows 3 levels but there can be more than 3 levels depending on the client creating the ticket.

I know this is not correct way to do this, and it's not finished:

data = {'tasks': 
            [
                {'id': 'xxxxxx1','parent': None},
                {'id': 'xxxxxx2','parent': 'xxxxxx1'},
                {'id': 'xxxxxx3','parent': 'xxxxxx2'},
                {'id': 'xxxxxx4','parent': 'xxxxxx2'},
                {'id': 'xxxxxx5','parent': None},
                {'id': 'xxxxxx6','parent': 'xxxxxx5'}
            ]
        }

taskData = {}
subTaskData = {}

for task in data['tasks']:
    ###task IDs with no parent
    if not task['parent']:
        taskData.setdefault(task['id'],[])
    else: 
        subTaskData.setdefault(task['id'],[""])

print(taskData)
print(subTaskData)

I would appreciate it if someone can help me with this using Python? I believe I can use something called recursive??

  • is the parent task always added before the child task? – Finn Feb 07 '22 at 10:29
  • What is the actual information you want to store? The subtasks will be nested dicts inside their parent task, but is there any other information? – Finn Feb 07 '22 at 10:34
  • @Finn yeah parent task is always added before the child task. There is other information such as title, description, due_date etc which would look this below which I need to store so I can display it later if it helps: {'id': 'xxxxxx1','parent': None,'name':'Ticket Title','Description':'I am a description','due_date':1643947200000}, – user3006202 Feb 07 '22 at 10:38
  • You could find it recursively, but in general you try to avoid recursive searchs if possible because they are not very efficient. I would add an `allchildren` in the `taskData` to avoid having to recursively search through all tasks – Finn Feb 07 '22 at 11:37
  • @Finn thanks for replying! Not sure how ```allchildren``` will help if I add it in the ```taskData``` do you have an example I can look into or something? Many thanks – user3006202 Feb 07 '22 at 11:47
  • Anyone can help? Really stuck with this. – user3006202 Feb 07 '22 at 21:09
  • i tried my approach but it didn't work as editing nested dicts in a loop is somewhat difficult if you dont know how many levels there will be – Finn Feb 08 '22 at 08:35
  • yeah thats what im struggling with :( hopefully someone else can help! – user3006202 Feb 08 '22 at 09:19

0 Answers0