'How to insert or change nested values in specific localization from one json into another in python
everytime I'd like to change some values in my game's config, I have to download the base file with 11k lines of values, then I have to do it manually. To simplify my issue:
With my exemplary base json
{
"assets":{
"config":{
"something": [ {
"myid" : "hehe",
"myip" : 0
}, {
"yourid" : "huehue",
"yourip" : 1
}],
"config2" : None
}
}
}
And my exemplary preset json
"assets":{
"config":{
"something" : [ {
"myid" : "hehe",
"myip" : 0
}, {
"yourid" : "huehue",
"yourip" : 1234234
}],
"config2" : None
"config3" : "yes"
},
"mamma_mia" : 0
}
}
I'd like the output that leave not used values the same, change values that are changed in the preset, and write the new ones. Since my real config is way more complicated, I am still thinking about even reaching the most nested values via some iteration, not by manual path. I'll grateful for any tips and tricks to get through json files <3
Solution 1:[1]
You can try dictionary merging :
import json
base = None
preset = None
with open('base.json', 'r') as f:
base = json.load(f)
with open('preset.json', 'r') as f:
preset = json.load(f)
merged = base | preset
But it might not work for your type of data.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marc_s |
