7 years ago
parsing response
Hello All,
I need some help on the python 3. I am getting device information in response.content. I need to display only id from it. How to parse the response.content. I am using Python 3.5 o...
CORRECTION to my last post. You'll want to iterate through 'items' list which is in the 'data' JSON-object.
8 hours ago, Joe Tran said:A GET for /device/devices should return a <class 'list'> object for the 'items' key in the 'data' JSON object or, as @Mike Moniz mentioned, will return 0 to n number of devices.
So either iterate through all the devices in 'data' or if you only care about the first device, reference the 0 index of 'data'
import requests [...] # use .json() method instead of .content jsonResponse = response.json() # Iterate through all devices in 'items' for device in jsonResponse['data']['items']: deviceID = device['id'] print('ID', str(deviceID)) # Only care about the first device in 'items' deviceID=(jsonResponse['data']['items'][0]['id']) print('ID', str(deviceID))