I coded this function change_settings_deep
which can take a deeper key (e.g. [key1, key2]
) as argument to change your value. BTW I also fixed an error in your original function that would lead to a ValueError: Circular reference detected
.
import json
from typing import Union, List
def change_settings(key: str, val: Union[str, int, dict]):
with open('somefile.json') as f:
data = json.load(f)
with open('somefile.json', 'w') as f:
data[key] = val
json.dump(data, f, indent=4)
# {"a": {"b": 7}, "c": 9}
change_settings('c', 'lol')
# {"a": {"b": 7}, "c": "lol"}
def change_settings_deep(keys: List[str], val: Union[str, int, dict]):
with open('somefile.json') as f:
data = json.load(f)
with open('somefile.json', 'w') as f:
data_cur = data
for key in keys[:-1]:
data_cur = data_cur[key]
data_cur[keys[-1]] = val
json.dump(data, f, indent=4)
# {"a": {"b": 7}, "c": 9}
change_settings_deep(['c'], 'bruh')
# {"a": {"b": 7}, "c": "lol"}
change_settings_deep(['a', 'b'], 'magic')
# {"a": {"b": "magic"}, "c": "bruh"}