0

I have tried everything I can possible come up with, but the value wont go away.

I have a JSON user and if user['permissions'] have key permission = "DELETE PAGE" remove that index of del user['permissions'][1] (in this example)

I want to have a list of possible values as "DELETE PAGE" and so on. If value in key, then delete that index.

Then return the users json without those items found.

I have tried del user['permission][x] and .pop() and so on but it is still there.

{
'id': 123,
'name': 'My name',
'description': 'This is who I am',
'permissions': [{
    'id': 18814,
    'holder': {
        'type': 'role',
        'parameter': '321',
        'projectRole': {
            'name': 'Admin',
            'id': 1,
        }
    },
    'permission': 'VIEW PAGE'
}, {
    'id': 18815,
    'holder': {
        'type': 'role',
        'parameter': '123',
        'projectRole': {
            'name': 'Moderator',
            'id': 2
        }
    },
    'permission': 'DELETE PAGE'
}]

}

Sebastian
  • 43
  • 1
  • 1
  • 10
  • 1
    Does this answer your question? [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Maurice Meyer Feb 08 '22 at 11:38
  • deleting in loop is not good idea beacause when you remove one item then next item is moved in this place. Better create new list/dictionary to keep elements which you want to keep. – furas Feb 08 '22 at 12:38

1 Answers1

0

Here's the code:

perm = a['permissions']
for p in perm:
    if p['permission'] == 'DELETE PAGE':
        perm.remove(p)
        
print(a)

Output:

{'id': 123, 'name': 'My name', 'description': 'This is who I am', 'permissions': [{'id': 18814, 'holder': {'type': 'role', 'parameter': '321', 'projectRole': {'name': 'Admin', 'id': 1}}, 'permission': 'VIEW PAGE'}]}
Devang Sanghani
  • 731
  • 5
  • 14