Home>

there is a json file:

"info": {
        "guilds": [{
            "guild": {
                "id": 926850130039148554,
                "name": "v1la | server",
                "user": 55
                "members": 53,
                "bots": 2,
                roles: {
                    "mute": 927842918851563520,
                    "voice_create": 927842909343064115,
                    "new_role": 927181380109074492
                },
                "channels": {
                    "voice_mod": 927255226371698758,
                    "join": 927340216761585675,
                    "leave": 927340302006636614
                }
            }
        }, {
            "guild": {
                "id": 821277749905195019,
                "name": "AnimEsHnik | clan",
                "user": 30
                members: 27
                "bots": 3,
                roles: {
                    "mute": 935937798614155294,
                    "voice_create": 929388475160096769,
                    "new_role": 929349231792246784
                },
                "channels": {
                    "voice_mod": 929392137135611984,
                    "join": 929379756888195093,
                    "leave": 929379792422305864
                }
            }
        }]
    }
}

when trying to display name with this code:


jjj= """
{
    info: {
        "guilds": [{
            "guild": {
                "id": 926850130039148554,
                "name": "v1la | server",
                "user": 55
                "members": 53,
                "bots": 2,
                roles: {
                    "mute": 927842918851563520,
                    "voice_create": 927842909343064115,
                    "new_role": 927181380109074492
                },
                "channels": {
                    "voice_mod": 927255226371698758,
                    "join": 927340216761585675,
                    "leave": 927340302006636614
                }
            }
        }, {
            "guild": {
                "id": 821277749905195019,
                "name": "AnimEsHnik | clan",
                "user": 30
                members: 27
                "bots": 3,
                roles: {
                    "mute": 935937798614155294,
                    "voice_create": 929388475160096769,
                    "new_role": 929349231792246784
                },
                "channels": {
                    "voice_mod": 929392137135611984,
                    "join": 929379756888195093,
                    "leave": 929379792422305864
                }
            }
        }]
    }
}"""
print(type(jjj))
data= json.loads(jjj)
for guild in data['info']['guilds']['guild']:
    print(guild['name'])

an error pops up: for guild in data['info']['guilds']['guild']: TypeError: list indices must be integers or slices, not str

what to do????

  • Answer # 1

    You havedata['info']['guilds']is a list. Therefore, python says that the list can only be accessed by integer keys.

    So you need something to style

    for guild_wrapper in data['info']['guilds']:
      guild= guild_wrapper['guild']
    

    thank you very much!

    ʏ9ᴅᴇʀ2022-01-29 14:23:46