I Just started writing this script for practice, it will take input and then write it inside Json file. Code looks like this:
import json
command = int(input("Do You Want To Add, Or Remove From List? 1/2 "))
if command == 1:
add_key = input("Type In key Name: ")
add_val1 = input("Type In Value N1: ")
add_val2 = input("Type In Value N2: ")
with open('jjj.json', 'r+') as jjson:
tvsh = json.load(jjson)
new_tvsh = {add_key: [add_val1, add_val2]}
tvsh.update(new_tvsh)
jjson.seek(0)
json.dump(tvsh, jjson)
elif command == 2:
with open('jjj.json', 'r+') as jjson:
tvsh = json.load(jjson)
chooseremove = input("Choose Key To Remove: ")
try:
del tvsh[chooseremove]
except KeyError as ex:
print(f"No such key: {chooseremove} ")
jjson.seek(0)
json.dump(tvsh, jjson)
Json File Looks Like This:
{"Key1":["Val1","Val2"],"Key2":["Val1","Val2"],"Key3":["Val1","Val2"]}
But when i try to remove key(For example key3) my json file will be like this:
{"Key1":["Val1","Val2"],"Key2":["Val1","Val2"]} "Key3":["Val1","Val2"]}
it's taking key outside dict but adding "}" at the end Any Ideas what can i do?
EDIT: Also Tried .pop but same result