Skip to content

Is there any way to search through a JSON object for a value? [closed]

I’m currently working on a ROBLOX Integrated discord bot with discord.py and have stumbled into an issue with checking if the user is in a group. I have already got a table of all groups and ranks, but have no clue how to iterate through it. The JSON structure is similar to as follows:

{
data:[{"group":{id:1,"name":"RobloHunks","memberCount":36351},"role":{"id":169,"name":"--","rank":1}},{"group":{id:2,"name":"LOL","memberCount":157765},"role":{"id":209,"name":"Cheezburgers","rank":1}}]
}

In a real example, there would be more than just two groups but I was wondering if there was any way to iterate through and check if the group ID is equal to 1 and then get the role associated with the group? I couldn’t find a solution anywhere else.

Answer

So basically, data is a key and the bracket block is a value. That value is a list of dictionaries and the group key corresponds to another dictionary this one containing the id.

So if the json is stored in a variable called x then doing

x[‘data’][0][‘group’][‘id’]

will get one of them. A loop would be something like

for i in range(len(x[‘data’])):
user_id = x[‘data’][i-1][‘group’][‘id’]
if user_id == ‘1’:
   print(x[‘data’][i-1][‘role’])

Hopefully this helps. The json would need to be correct though!