I have JSON file with the following structure:
{'1234': 'ABC', '2345': 'BCD', '3456': 'CDE'}
I want to append the key value pair : {'4567': 'DEF'}
to this JSON file so that it looks like:
{'1234': 'ABC', '2345': 'BCD', '3456': 'CDE', '4567': 'DEF'}
This is the code I have but it adds the key: value pair as another dictionary like structure:
new_data = {'4567': 'DEF}
j = json.dumps(new_data)
with open('hashDir.json','r+') as file:
file.write(j)
file.close()
r+ : Opens a file for reading and writing, placing the pointer at the beginning of the file. w : Opens in write-only mode. The pointer is placed at the beginning of the file and this will overwrite any existing file with the same name. It will create a new file if one with the same name doesn't exist. – gowridev May 31 '21 at 01:55