Dorothy
Dorothy

Reputation: 61

Python Error When Parsing JSON

So I am trying to find the IP address from this JSON:

[{"id":"001788fffe48cbdb","internalipaddress":"192.168.1.102"}]

So my code to do this is:

r_lights = requests.get('https://www.meethue.com/api/nupnp')

js= r_lights.json()                                                                     

hue_lights = [js[index]["name"] for index in js]

However, it returns the error:

TypeError: list indices must be integers or slices, not dict

I've used that code to successfully search other JSON in my code, but I'm not sure how to change it to work here.

Upvotes: 0

Views: 48

Answers (2)

JZ.
JZ.

Reputation: 21877

Use the string values of the dictionaries directly:

for obj in index['name']:
    hue_lights = obj['one']['two'] 

Upvotes: 0

Mithilesh Gupta
Mithilesh Gupta

Reputation: 2930

Try:

hue_lights = [index["name"] for index in js]

or if you want ip's

hue_lights = [index["internalipaddress"] for index in js]

Upvotes: 1

Related Questions