I am trying to load in a JSON file via an API which returns the following:
{
"success": true,
"message": "",
"data": {
"account_sid": "ABCDEFGHIJK",
"sites": [
{
"name": "1",
"devices": [
{
"serial": "12345678",
"name": null,
"instruments": [
{
"serial": "WWW",
"name": "DEV1"
},
{
"serial": "XXX",
"name": "DEV2"
},
{
"serial": "YYY",
"name": "DEV3"
},
{
"serial": "ZZZ",
"name": "DEV4"
}
]
}
]
}
]
}
}
What I am trying to do is access the "instruments"
section and store each serial within a dictionary. So: data -> sites -> devices -> instruments
.
I am trying to do this by:
response = requests.post(URL)
returned_data = json.loads(response.text.encode().decode('utf-8-sig'))
print(json.dumps(returned_data, indent=4))
devices = dict()
for device in returned_data['data']:
device_id = device['sites']['devices']['instruments']['serial']
device_name = device['sites']['devices']['instruments']['name']
devices[device_name] = device_id
However, on each attempt to run, I receive the following error:
Exception has occurred: TypeError
string indices must be integers
File "C:\Users\me\Documents\file.py", line 55, in <module>
device_id = device['sites']['devices']['instruments']['serial']
I am relatively new to python, so any pointers would be a great help.