Take one item from a json list and append it to another

I am making a simple todo app with Qt in Python 3. I am using JSON to store the todo list data.

Here is the format of my JSON:

Category (todo, inprog or done). Then a list of tasks with 3 fields. name (str), desc (str), due (str).

Here is an example of the JSON I am dealing with:

{
  "todo": [
    {
      "name": "todo-test",
      "desc": "just a test task",
      "due": "DD/MM/YYYY HH:MM:SS"
    },
    {
      "name": "todo-test2",
      "desc": "just a test task",
      "due": "DD/MM/YYYY HH:MM:SS"
    }
  ],
  "inprog": [
    {
      "name": "inprog-test",
      "desc": "just a test task",
      "due": "DD/MM/YYYY HH:MM:SS"
    },
    {
      "name": "inprog-test2",
      "desc": "just a test task",
      "due": "DD/MM/YYYY HH:MM:SS"
    }
  ],
  "done": [
    {
      "name": "done-test",
      "desc": "just a test task",
      "due": "DD/MM/YYYY HH:MM:SS"
    },
    {
      "name": "done-test2",
      "desc": "just a test task",
      "due": "DD/MM/YYYY HH:MM:SS"
    }
  ]
}

As you can see, it is very simple.

I need some help with moving an item from (for example) todo to inprogress. Here is my current, unfinished function.

    def moveCheckbox(self, cb, state):
        """Deletes specified task (cb) from to-do.json and re-adds it
        in the new category based on the state (state) of the checkbox.
        """
        todo_text = cb.text()
        todo_json = self.validate_todo_json()
        print(todo_json)

        if state == 0:
            for item in todo_json['todo']:
                if item.name == todo_text:
                    todo_json['todo'].remove(item)
                    todo_json['inprog'].append(item)

I am using checkboxes to represent the todo items. For people who are unfimiliar with Qt, state of 0 = unchecked, state of 1 = partial check, state of 2 = checked. This is the base layout of the current UI:

Each category is a state of checkbox.
In short, how can I move an item from the ‘todo’ category from my JSON, to the ‘inprog’ category (or from/to any other category)

JSON is just how you’re storing them; these are simply lists. What you have ought to work, but you may need to break after you do this manipulation, as otherwise you may run into problems.

TIP: Format your due dates as "YYYY-MM-DD HH:MM:SS" rather than using American dates. That way, you can sort and compare them. If you want to be really fancy, replace the space with the letter “T” and call it ISO 8601 :slight_smile:

When I originally had this code, it would just clear the file. Know I know this should work, when I get home, I shall test some JSON things in a separate python file just in case (my current file is 1000+ lines so it could be anything…)

You basically have three lists of dictionaries, You access a list by index (index 0 will be the first dict).
You likely want to determine the index of the dictionary you want to move between two lists.
I would first add the item to the desired list and remove it afterward.
Simplify your example as much as possible and build from there.
If you can, I would avoid comparing strings to know if a task has changed, I would use in the dict a key like desired_state: 0|1|2 or using booleans like: is_task_done: True | is_task_partially_done: False | is_task_to_be_done: True.
The UI would simply change those values based on the checkbox! Your code would avoid string comparisons!
It’s always easier to use date and time formats that are well understood.

That’s a very C mentality. In Python, we don’t mind using human-readable strings.

An example of what, I think, you are stuck on with a Python mentality:

import json

todo_text = 'todo-test'
state = 0

with open('./t.json', 'r') as file:
    todo_json = json.load(file)

state_as_names= ['todo', 'inprog', 'done']

for tasks in todo_json.values():
    for index, task in enumerate(tasks):
        if todo_text == task['name']:
            todo_json[state_as_names[state]].append(task)
            tasks.pop(index)
print(todo_json)

The above code, does not take into consideration when there is nothing to do (the state of the checkbox does not change)!
t.json is the JSON file you provided.
This is a fully working block of code, does it do what you want is something else!!! :slight_smile: A stand-alone code and an example of the output you desire is best.