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?

5
  • Nothing at all to do with JSON, this is a fundamental property of Python. Commented Jan 30, 2019 at 0:46
  • There's also not a null value in Python. It turns into None fyi Commented Jan 30, 2019 at 0:48
  • @Jaba Oops, sorry yea i pasted the JSON response. But when i do json.loads it should convert it to none right? Commented Jan 30, 2019 at 0: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. Commented Jan 30, 2019 at 0:50
  • Yes it will, I assumed that's what happened Commented Jan 30, 2019 at 0:51

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

8 Comments

That worked, now i want to dynamically assign the generated output to variables. Like, a=blah ,b=blah1 , c=ok and d=ok. Even if there are 4 lines, i want to assign them dynamically, hope i made sense.
ok i will update and include that in the answer
@Bharath are you sure you want to assign variables to the data, would a dictionary also work?
@Bharath see updated answer. it's using a dictionary to get "variable" values. Let me know if this does not meet your needs and we can try something else.
Alright, thanks for your feedback, I'll take a look and fix it ASAP
|
2

I tried the below and it worked:

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

Comments

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"

1 Comment

Thanks very much, i will try this now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.