JSON save file: finding, editing, removing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By FT_Anx

Hi,

I have a JSON save file which is something like this below. There’s the goals, the tasks and the milestones, one is inside another.

That’s my first project, so I struggled with it quite for a while. Now I know I could use way less lists in this file and use dictionaries instead, but now there’s so much code and the problem was in adding them to the file, something happened that I couldn’t solve it (I can’t quite remember what, a while ago). Using “.append()” worked and I ended up using it, funnily enough.

[
  {
    "goal_name": "Game Dev",
    "goal_creation_date": "",
    "tasks": [
      {
        "milestones": [
          {
            "milestone_archived": false,
            "milestone_deadline_day": "",
            "milestone_priority": "highest",
            "milestone_title": "Brainstorm: boss - super powers",
          },
          {
            "milestone_archived": true,
            "milestone_deadline_day": "",
            "milestone_priority": "average",
            "milestone_title": "Brainstorm: ideas"
          }
        ],
        "task_deadline": "",
        "task_description": "Create the first level of your game",
        "task_priority": "high",
        "task_title": "Game #1 - Level 1"
      },
      {
        "milestones": [
          {
            "milestone_archived": false,
            "milestone_deadline_day": "",
            "milestone_priority": "high",
            "milestone_title": "Brainstorm: character backgrounds",
          },
          {
            "milestone_archived": false,
            "milestone_deadline_day": "",
            "milestone_priority": "average",
            "milestone_title": "Research: character names",
          }
        ],
        "task_deadline": "",
        "task_description": "",
        "task_priority": "high",
        "task_title": "Game #1 - Character Design"
      }
    ]
  }
]

My problem is: in a singleton, I made a function that searches for a specific milestone of a specific task of a specific goal.
I run a ‘for’ loop, part of it being something like:

var counter = 0
	for key in json_file:

		if key["title"] == "%s" %goal_title: 
                 # Comparing to see if I've found a specific goal title.
			print( "The goal title has been found." )
	if key['tasks'][task_number]["task_title"] == "%s" %task_title:
		# task_number is a var which has the right task position in the list.
			print( "The task title has been found." )
			
		counter += 1
		

The output shows that only goal_title was found, but sends an error message: “Invalid get index ‘tasks’ (on base: ‘Dictionary’).” What’s the right way to make this work?

Some other questions:
(1) How can I find the milestone “Brainstorm: character backgrounds”, for example?
(2) How can I edit a task without changing its milestones?
(3) How can I remove any task or milestone?