Removing nested JSON key from json file in python
问题 Im using python 3 and i try to delete key and value in a json file. settings: {"debug": "0", "watchdog":{ "interval": 10, "services": { "record": { "lag": 5 }, "gps": { "lag": 60 }, "ntp": { "lag": 120 } } } } Im trying to delete key and value from a file if key exists. My code: import os import json service = "ntp" with open('settings.json', 'r') as dataFile: data = json.load(dataFile) if service in data["watchdog"]["services"]: del data["watchdog"]["services"][service] with open('settings