2

I want to iterate through a JSON response to match specific key value pair to print it and another value in the same list.

The JSON looks like

[
{
    "status": "ok",
    "slot": null,
    "name": "blah",
    "index": 0,
    "identify": "off",
    "details": null,
    "speed": null,
    "temperature": null
},
{
    "status": "ok",
    "slot": null,
    "name": "blah1",
    "index": 0,
    "identify": "off",
    "details": null,
    "speed": null,
    "temperature": null
},
{
    "status": "ok",
    "slot": null,
    "name": "blah2",
    "index": 1,
    "identify": "off",
    "details": null,
    "speed": null,
    "temperature": null
}
]

The code i tried so far:

 url = http://something
 data = json.loads(r.get(url, headers=headers, verify=False).content.decode('UTF-8'))

 for value in data:
   if value['name'] == 'blah' or 'blah1':
        print(value)

And i tried with a next gen:

match = next(d for d in data if d['name'] == 'blah')
yield next(match)

But none of this worked.

The desired output i want is: If the 'name'='blah' or 'name'='blah1', i want to print out name and the associated status with it.

'name'='blah' 'status'='ok'

'name'='blah1' 'status'='ok'

How can i do it with Python?

Bharath
  • 559
  • 11
  • 27
  • Nothing at all to do with JSON, this is a fundamental property of Python. – roganjosh Jan 30 '19 at 00:46
  • There's also not a `null` value in Python. It turns into `None` fyi – Jab Jan 30 '19 at 00:48
  • @Jaba Oops, sorry yea i pasted the JSON response. But when i do `json.loads` it should convert it to `none` right? – Bharath Jan 30 '19 at 00:49
  • Yes, it will, but the answer is not predicated on that. It's a duplicate; you're not making the correct `if` checks. The JSON component makes it ripe for people to answer a regular Python question rather than flag as a dupe. – roganjosh Jan 30 '19 at 00:50
  • Yes it will, I assumed that's what happened – Jab Jan 30 '19 at 00:51

3 Answers3

2

I tried the below and it worked:

for value in data:
   if value['name'] == 'blah' or value['name'] == 'blah1':
      print(value)
Amir Imani
  • 3,118
  • 2
  • 22
  • 24
2

for each 'name' value check if it is in the valid values:

for value in data:
    if value['name'] in ['blah', 'blah1']:
        print(value['name'], value['status'])

output:

u'blah', u'ok'
u'blah1', u'ok'

Update to question in comment: To dynamically assign values to variable names, the most pythonic way to do this may be to use a dictionary to assign each variable name as the dictionary's keys and the corresponding values (taken from this answer How can you dynamically create variables via a while loop? [duplicate]):

import string

var = string.ascii_lowercase

d = {}
k = 0

for value in data:
    d[var[k]] = value['name']
    k += 1
    d[var[k]] = value['status']
    k += 1

Now we have a dictionary of variable names as keys that we can get their assigned values:

print(d['a']) # blah
1

You're using next on the value you get from next already. I'd use something like this:

data = [
{
    "status": "ok",
    "slot": None,
    "name": "blah",
    "index": 0,
    "identify": "off",
    "details": None,
    "speed": None,
    "temperature": None
},
{
    "status": "ok",
    "slot": None,
    "name": "blah1",
    "index": 0,
    "identify": "off",
    "details": None,
    "speed": None,
    "temperature": None
},
{
    "status": "ok",
    "slot": None,
    "name": "blah2",
    "index": 1,
    "identify": "off",
    "details": None,
    "speed": None,
    "temperature": None
}
]

#This is the part you're trying to work
def get_name(data, name):
  return next(d for d in data if d.get('name', None) == name)

v = get_name(data, "blah")
if v:
  print(f"Name: {v['name']}, Status: {v['status']}")
else:
  print("No Value")

This prints:

"Name: blah, Status: ok"
Jab
  • 26,853
  • 21
  • 75
  • 114