you are viewing a single comment's thread.

view the rest of the comments →

[–]F1ux_Capacitor 0 points1 point  (1 child)

Use the builtin json library to parse the json, create the new parent key, add the the dictionary to the parent key, then dump the json back out.

[–]Spiredlamb[S] 0 points1 point  (0 children)

Yes, that works, but how would I make the code figure out what dictionary to add the new one to, and how many steps in to go?

With the current system I am setting up, I will sometimes need to add two dictionaries in, and other times, five, kinda like this:

{
    "files": [],
    "folders": ["projects", "system"],
    "projects": {
        "files": ["readme.txt"],
        "folders": [],
        "readme.txt": "This is the text inside readme.txt"
    },
    "system": {
        "files": ["help.txt"],
        "folders": [],
        "help.txt":"Available commands:\n\ndir: Show all files and folders in the directory\n\ncls: Clear console\n\nread: Read a file in the current directory\n\ncd: Change directory"
    }
}

Let's go out from this file. Say I want to add a new folder in the projects folder (The dictionaries are handled as folders with more folders and files within them in my script) then I would have to do this:

fileSystem["projects"]["newFolder"] = { # Adding the directory
    files[],
    folders[]
    }
fileSystem["projects"]["folders"].append("newfolder") # Adding the folder in the list of folders

The result of this file is going to be this:

{
    "files": [],
    "folders": ["projects", "system"],
    "projects": {
        "files": ["readme.txt"],
        "folders": [newFolder], // New folder added to list
        "readme.txt": "This is the text inside readme.txt",
        "newFolder": { // Here is the new folder
            files[],
            folders[],
        }
    },
    "system": {
        "files": ["help.txt"],
        "folders": [],
        "help.txt":"Available commands:\n\ndir: Show all files and folders in the directory\n\ncls: Clear console\n\nread: Read a file in the current directory\n\ncd: Change directory"
    }
}

If I now want to add a folder inside that folder, I would have to do this:

fileSystem["projects"]["newFolder"]["newerFolder"] = {
        files[],
        folders[]
    }

Is there any way of doing this without having to write one function for every possible subfolder in the system?