I have json file something like this one.
{
"SomethingA": {
"SomethingB": {
"SomethingC": {
"C-property": "something",
"C-property2": {}
}
}
}
}
I want to add some new data top of the the "Something C" as "NEWSomethingC"
so It should be
{
"SomethingA": {
"SomethingB": {
"NEWSomethingC": {
"NEWC-property": "NEWsomething",
"NEWC-property2": {}
},
"SomethingC": {
"C-property": "something",
"C-property2": {}
}
}
}
}
Okay, here is the problem. I can't add new value top of the keys. Always, NEWSomethingC is going to appear below the SomethingC.
The code I use for adding...
with open(credantials.init['config'], 'r+') as f:
data = json.load(f)
try:
old_data = data['SomethingA'][SomethingB]
append_data = data['SomethingA'][SomethingB]
old_data = {NEWSomethingC :{'C-property':something, 'C-Property2':{}}}
except KeyError:
print ('There is no key you want to search here')
append_data.update(old_data)
print(append_data)
f.seek(0)
json.dump(data,f, indent=4)
f.truncate()