Home > Net >  python dictionary update without overwriting
python dictionary update without overwriting

Time:02-01

I am learning python, and I have two json files. The data structure in these two json files are different structures.

I start by importing both of the json files. I want to choose a course from the courses dict, and then add it to a specific education in the educations dict. What I want to solve is via user input choose a key from the first dict, and then within a while loop, so I can add choose a key from the second dict to be added to the dict chosen from the first dict.

I am able to add the dict from the second dict to the one first as a sub dict as I want to, but with the update method it overwrites all previous values.

I have used the dict.update() method so not to overwrite previous values. I then want to write back the updated dict back to the first json file.

My code works partially, I am able to add a course to a educations, but it overwrites all previous courses I chose to add to a specific education.

This is the content of the first json file:

{
    "itsak22": {
        "edcuationId": "itsak22",
        "edcuation_name": "cybersecurityspecialist"
    },
    "feu22": {
        "edcuationId": "feu22",
        "edcuation_name": "frontendutvecklare"
    }
}

This is the content of the second json file:

{
    "sql": {
        "courseId": "itsql",
        "course_name": "sql",
        "credits": 35
    },
    "python": {
        "courseId": "itpyt",
        "course_name": "python",
        "credits": 30
    },
    "agile": {
        "courseId": "itagl",
        "course_name": "agile",
        "credits": 20
    }
}

And this is my python code:

import json

# Load the first JSON file of dictionaries
with open('edcuations1.json') as f:
    first_dicts = json.load(f)

# Load the second JSON file of dictionaries
with open('courses1.json') as f:
    second_dicts = json.load(f)

# Print the keys from both the first and second JSON files
print("All educations:", first_dicts.keys())
print("All courses:", second_dicts.keys())

# Ask for input on which dictionary to add to which
first_key = input("Which education would you like to choose to add courses to? (Enter 'q' to quit): ")

while True:
    second_key = input("Which course would you like to add to education? (Enter 'q' to quit)")
    if second_key == 'q':
        break

    # Create a sub-dictionary named "courses" in the specific dictionary of the first file
    if "courses" not in first_dicts[first_key]:
        first_dicts[first_key]["courses"] = {}
    first_dicts[first_key]["courses"].update(second_dicts[second_key])
    #first_dicts = {**first_dicts, **second_dicts}
    #first_dicts.update({'courses': second_dicts})



# Update the first JSON file with the new dictionaries
with open('edcuations1.json', 'w') as f:
    json.dump(first_dicts, f, indent=4)

CodePudding user response:

I'm not entirely sure how your desired result should look like but I think your dictionary courses should be a list and not a dictionary.

Then you can do

if "courses" not in first_dicts[first_key]:
    first_dicts[first_key]["courses"] = []
first_dicts[first_key]["courses"].append (second_dicts[second_key])

And your result looks like this if you add all courses to itsak22

{
    "itsak22": {
        "edcuationId": "itsak22",
        "edcuation_name": "cybersecurityspecialist",
        "courses": [
            {
                "courseId": "itsql",
                "course_name": "sql",
                "credits": 35
            },
            {
                "courseId": "itpyt",
                "course_name": "python",
                "credits": 30
            },
            {
                "courseId": "itagl",
                "course_name": "agile",
                "credits": 20
            }
        ]
    },
    "feu22": {
        "edcuationId": "feu22",
        "edcuation_name": "frontendutvecklare"
    }
}
  • Related