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
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.
Here is my approach: At each level, update the value. Then recursively traverse into the value’s values and update them as well.
Notes
updated_value = new_data.get(key, {})
gets the updated value. If the key is not found innew_data
, we got ourselves an empty dictionary.updated_value
. Ifupdated_value
is an empty dictionary, nothing will get updated.