14

I'm using a website's API 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:

{
  "success":true,
  "data":
  {
    "array":
    [
      {
        "id":"1","name":"Value1"
      },
      {
        "id":"2","name":"Value2"
      }
    ]
  }
}

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)
else:
    print ('Nothing was found.')

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

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

All help is greatly appreciated.

5
  • 1
    I think you need to look at the json librairy for python. r.json() gives a string in a form of a json. You need to dump the string to obtain a dict like object Commented Jun 30, 2016 at 6:23
  • 1
    @Whitefret Assuming r is obtained by request.get(), r.json() returns a Python dictionary. Commented Jun 30, 2016 at 6:24
  • 1
    What exactly are you trying to do? Commented Jun 30, 2016 at 6:24
  • 1
    @DeepSpace thanks for the precision. I guess it should work then... Commented Jun 30, 2016 at 6:25
  • 1
    Alright I look into the documentation. I try to take shortcuts, I have not read any Python/Json documentation I just dove right into it. I have grasped the basics (so I think). However I'm missing out on a lot of things that I should know. Commented Jun 30, 2016 at 6:25

3 Answers 3

21

Here is a one-liner example for searching:

aaa = {
  "success":True,
  "data":
  {
    "array":
    [
      {
        "id":"1","name":"Value1"
      },
      {
        "id":"2","name":"Value2"
      }
    ]
  }
}

[a['name'] for a in aaa['data']['array'] if a['id']=='1']

This will return all the found cases, or an empty array if nothing is found

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

Comments

10

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)
        break
else:
    print('Nothing found!')

Comments

8

Your code can be simplified a lot:

# no need to call `r.json` so many times, can simply save it to a variable
json_data = r.json()
for item in json_data["data"]["array"]:
    if item["name"] == "Value1":
        # do something...

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.