0

I have the following .json file named weapons.json:

{
  "weapons": [
    {
      "game": "EMPTY",
      "weapon": "EMPTY",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    },
    {
      "game": "DELETE",
      "weapon": "DELETE",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    }
  ]
}

This data is only for testing.

From this file I want to remove the second item :

{
  "game": "DELETE",
  "weapon": "DELETE",
  "down": "0",
  "up": "0",
  "left": "0",
  "right": "0"
}

For this, I have tried the following function:

#Deleting json info from the selected weapon
def deleteWeapon():
        with open('json/weapons.json') as info:
                weapons_dict =  json.load(info)
        
        for weapon in weapons_dict['weapons']:
                if (weapon['game']=="DELETE" and weapon['weapon']=="DELETE" and weapon['down']=="0" and weapon['up']=="0" and weapon['left']=="0" and weapon['right']=="0"):
                        del weapon
                        
        with open('json/weapons.json','w') as remove:
                json.dump(weapons_dict,remove,indent=2)
                
deleteWeapon()

Where I try to do a del weapon to remove the value, but this does not work. I have also tried doing del weapon['XXX'] del weapon['XXX'] with all item values, but doing this the .json looks like this.

{
  "weapons": [
    {
      "game": "EMPTY",
      "weapon": "EMPTY",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    },
    {}
  ]
}

Instead of :

{
  "weapons": [
    {
      "game": "EMPTY",
      "weapon": "EMPTY",
      "down": "0",
      "up": "0",
      "left": "0",
      "right": "0"
    }
  ]
}

I am grateful to hear any help.

5
  • If any clarification is needed I am awaiting comments. Commented Sep 15, 2021 at 13:40
  • Try to use weapons_dict['weapons'].pop(weapon) instead of 'del weapon' Commented Sep 15, 2021 at 13:54
  • @Wimanicesir I don't think it's safe to remove items from a list that you are currently iterating. Also I think that .pop(weapon) will fail as weapon isn't an int. I think you meant .remove(weapon) Commented Sep 15, 2021 at 13:58
  • @Alexci Please remove the tkinter tag from the question as the question isn't directly linked to tkinter. Commented Sep 15, 2021 at 13:59
  • @TheLizzard, already done Commented Sep 15, 2021 at 14:12

2 Answers 2

1

Try this:

weapons = list(weapons_dict["weapons"])
for i, weapon in enumerate(weapons):
    if (weapon['game']=="DELETE" and weapon['weapon']=="DELETE" and weapon['down']=="0" and weapon['up']=="0" and weapon['left']=="0" and weapon['right']=="0"):
        del weapons_dict["weapons"][i]

That iterate over the list using i as the current index and then it will call del weapons_dict["weapons"][i] when ever it needs to delete that weapon.

Sign up to request clarification or add additional context in comments.

3 Comments

Nope, still don't remove the value.
@Alexci Btw I fixed the problem.
Yep, it's fixed, your solution remove data from json, also mine remove data from json and directly from my Treeview.
1

Already found the solution.

The code should look like this :

def deleteWeapon():
        with open('json/weapons.json') as info:
                weapons_dict =  json.load(info)
        
        weapons = list(weapons_dict["weapons"])
        for i, weapon in enumerate(weapons):
                if (weapon['game']=="DELETE" and weapon['weapon']=="DELETE" and weapon['down']=="0" and weapon['up']=="0" and weapon['left']=="0" and weapon['right']=="0"):
                        weapons_dict['weapons'].pop(i)
                        
        with open('json/weapons.json','w') as remove:
                json.dump(weapons_dict,remove,indent=2)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.