skip to Main Content

Have 2 JSON files:
First file is:

{
  "Bob": {
    "tags": ["theme1", "theme2", "theme3"
    ]},
  "John": {
    "tags": ["theme2", "theme3"
    ]}
}

and second file is:

{
    "level1": {
        "level2": {
            "level3": {
                "Bob": {
                    "gear": "Yes"
                },
                "John": {
                    "gear": "None"
                },
                "Marcel": {
                    "gear": "N/A"
                }
            }
        }
    }
}

Trying to merge both in a third one like:

{
    "level1": {
        "level2": {
            "level3": {
                "Bob": {
                    "gear": "Yes",
                    "tags": ["theme1", "theme2", "theme3"]
                    
                },
                "John": {
                    "gear": "None",
                    "tags": ["theme2", "theme3"]
                },
                "Marcel": {
                    "gear": "N/A",
                }
            }
        }
    }
}

Deeper level of second file cause me troubles. Output file needs to be a valid JSON.
Thanks for any advice.

I tried this:

# Read data from the first JSON file
with open('file1.json', 'r') as f1:
    data1 = json.load(f1)

# Read data from the second JSON file
with open('file2.json', 'r') as f2:
    data2 = json.load(f2)

# Merge the data based on common keys
merged_data = {}

for key in data1:
    #print(key)
    for level1 in data2:
        #print(level1)
        if key != level1:
            for level2 in data2[level1]:
                #print(level2)
                if key != level2:
                    for level3 in data2[level1][level2]:
                        #print(level3)
                        if key != level3:
                            for level4 in data2[level1][level2][level3]:
                                #print(level4)
                                if key == level4:
                                    merged_data[key] = {"gear": data1[key]["gear"], **data2[level1][level2][level3][key]}




merged_data = f"{data2} + {merged_data} }}}}}}"

merged_data = json.dumps(merged_data.read_vms_tags())

with open('merged_json.json', 'w') as merged_file:
    json.dump(merged_data, merged_file, indent=4)

The best I could get was having merged file with same level.

Chris, thanks advice to edit my post 🙂

2

Answers


  1. import json
    
    # Load the first JSON file
    with open('file1.json', 'r') as f:
        file1 = json.load(f)
    
    # Load the second JSON file
    with open('file2.json', 'r') as f:
        file2 = json.load(f)
    
    # Merge the two JSON files
    for name, data in file1.items():
        if name in file2['level1']['level2']['level3']:
            file2['level1']['level2']['level3'][name].update(data)
    
    # Save the merged JSON to a new file
    with open('merged_file.json', 'w') as f:
        json.dump(file2, f, indent=4)
    

    This code loads the two JSON files, merges them by updating the data for each name in the second file with the data from the first file, and then saves the merged data to a third JSON file.

    Login or Signup to reply.
  2. Here is my approach: At each level, update the value. Then recursively traverse into the value’s values and update them as well.

    import json
    
    
    def update(new_data, target):
        for key, value in target.items():
            if not isinstance(value, dict):
                continue
    
            # Update the value
            updated_value = new_data.get(key, {})
            value.update(updated_value)
    
            # Since value is a dictionary, update its values recursively
            update(new_data, value)
    
    
    # This is the new data
    with open("file1.json", "r", encoding="utf-8") as stream:
        data1 = json.load(stream)
    
    # This is the data to be updated
    with open("file2.json", "r", encoding="utf-8") as stream:
        data2 = json.load(stream)
    
    # Update
    update(data1, data2)
    with open("merged_json.json", "w", encoding="utf-8") as stream:
        json.dump(data2, stream, indent=4)
    

    Notes

    • The statement updated_value = new_data.get(key, {}) gets the updated value. If the key is not found in new_data, we got ourselves an empty dictionary.
    • The next step is to update the value using updated_value. If updated_value is an empty dictionary, nothing will get updated.
    • The important part of the update function is when value is a dictionary, we call update on that value in order to recursively update that value’s values.
    • This function works regardless of how many levels the the user’s data is in.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search