如何从 JSON 中删除键和值

How to delete key and value from JSON

我正在为我的机器人制作标签功能,并且我已经设法发出命令来创建、使用和显示标签。现在我坚持如何根据给定的标签删除键和值。例如 !deletetag youtube 将从下面的列表中删除 youtube 标签:

{
    "twitch": "https://www.twitch.tv/",
    "youtube": "https://www.youtube.com/",
    "ig": "https://www.instagram.com/",
    "twitter": "https://twitter.com/"
}

我使用 JSON 文件来存储标签的原因是因为该机器人将仅在一个服务器中使用 <100 人并且它不会包含超过 30-40 个标签。这是我尝试过的:

@commands.command(name="deletetag",
                  help="Delete an existing tag.")
async def _deletetags(self, ctx, tag: str):
    with open('tags.json') as output:
        data = json.load(output)
        for key in data:
            del key[tag]

将我的评论作为答案,以便于参考。

# Remove key from dictionary
del data[tag]

# Serialize data and write back to file
with open('tags.json', 'w') as f: 
    json.dump(data, f)