I’m making a JSON change detector that lets the user either accept or decline the changes.
import json
import time
def detect_new_keys(file_path):
existing_data = {}
with open(file_path, 'r') as file:
existing_data = json.load(file)
keys = []
for key in existing_data.keys():
keys.append(key)
while True:
new_data = {}
with open(file_path, 'r') as file:
new_data = json.load(file)
new_keys = []
for key in new_data.keys():
new_keys.append(key)
new_keys_only = [key for key in new_keys if key not in keys]
print("New keys added:", new_keys_only)
time.sleep(5)
detect_new_keys('reports.json')
The provided code doesn’t find the changes. Please add if user input is n
it deletes the key.
I expect it to say “New keys added: newKey”
New contributor
cool1001 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.