Zach Long Zach Long - 1 year ago 71
Python Question

How to search for specific value in Json array using Python

I'm using a website's

to get information. It outputs 100 values.

What is the best practice of searching for a specific value in this array?

The array looks something like this:


I try searching for the data using this snippet I found elsewhere, however it does not work:

for name in (r.json()['data']['array'][0]['name']):
if r.json()['data']['array'][0] == s_name:
ident = r.json()['data']['array'][0]['id']
name = r.json()['data']['array'][0]['name']
print (name + ' - ' + ident)
print ('Nothing was found.')

So if I was trying to reference
how would I do this using

Please note: I'm a rookie developer, first time using
, not very experienced with

All help is greatly appreciated.

Answer Source

Not sure where that piece of code came from but it looks very wrong.

Just looking at the structure you can do something like:

for attrs in r.json()['data']['array']:
    if attrs['name'] == s_name:
        ident = attrs['id']
        name = attrs['name']
        print(name + ' - ' + ident)
    print('Nothing found!')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download