Skip to content
Advertisement

How can I remove a part of the JSON, and can someone explain how It works?

So I’m new to the JSON files, I was trying to make that my python program deletes a JSON “line” when something happens, I got the code that write in the JSON but I don’t know how it works, I would appreciate that someone explains how it works:

with open('guilds.json', 'r', encoding='utf-8') as f:
    guilds_dict = json.load(f)
guilds_dict[str(ctx.guild.id)] = str(channel.id)
with open('guilds.json', 'w', encoding='utf-8') as f:
    json.dump(guilds_dict, f, indent=4, ensure_ascii=False)
await ctx.send(f'Seleccionado el canal {channel.name} como canal de Bienvenida de {ctx.message.guild.name}') 

And the JSON looks like this:

{
    "692539777958019103": "692539778494890027",
    "972102908529225758": "972102909372297228"
}

How could I do to for example if the var guid has 972102908529225758 how can I delete this part? "972102908529225758": "972102909372297228"

Advertisement

Answer

this is like a dictionary so a simple guilds_dict.pop(‘Your key’) would be enough;

How could I do to for example if the var guid has 972102908529225758 how can I delete this part? “972102908529225758”: “972102909372297228”

guilds_dict.pop(‘972102908529225758’);

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement