I have a JSON configuration file which looks something like this:
{
"generic": {
"loglevel": 2,
...
},
"clients": [
{
"type": "foo",
"bar": {
"bar_1": 0.7,
"bar_2": 0.95
},
...
},
{
"type": "foo",
...
}
]
}
I can modify the contents and store the modified version of it using:
import json
with open("sample.cfg", "r") as config_file:
config = json.load(config_file)
config["clients"][0]["bar"]["bar_1"] = 100
with open("modified.cfg", "w") as config_file:
config_file.write(json.dumps(config))
But I would like to modify the file based on some input. Let's say the input is a string changestring:
changestring = 'clients,0,bar,bar_1:1,2,3'
keyval=changestring.split(':')
keys = keyval[0].split(',')
vals = keyval[1].split(',')
But now I don't know how to use the keys in order to access the config path. Is this actually the right way to do this? Or maybe there is a different way to handle it? Thanks.