I'm parsing data from the fitbit API (https://dev.fitbit.com/build/reference/web-api/heart-rate/) and keep getting TypeError: string indices must be integers. I understand, based off of reading other questions, that this could be due to dealing with lists of dictionaries. Right now, I'm parsing my json like this:
fat_burn = "{0:.2f}".format(activity_request.json()['activities-heart'][0]['value']['heartRateZones'][1]['minutes'])
My json looks like this:
{
"activities-heart": [
{
"dateTime": "2015-08-04",
"value": {
"customHeartRateZones": [],
"heartRateZones": [
{
"caloriesOut": 740.15264,
"max": 94,
"min": 30,
"minutes": 593,
"name": "Out of Range"
},
{
"caloriesOut": 249.66204,
"max": 132,
"min": 94,
"minutes": 46,
"name": "Fat Burn"
},
{
"caloriesOut": 0,
"max": 160,
"min": 132,
"minutes": 0,
"name": "Cardio"
},
{
"caloriesOut": 0,
"max": 220,
"min": 160,
"minutes": 0,
"name": "Peak"
}
],
"restingHeartRate": 68
}
}
]
}
I had an error that looked like TypeError: list indices must be integers or slices, not str when I was parsing the json like:
fat_burn = "{0:.2f}".format(activity_request.json()['activities-heart']['value']['heartRateZones'][1]['minutes'])
but managed to resolve it by adding a [0].
Why did I need to index the first entry to solve that error, and what do I need to do to resolve the error I'm having now?
activities-heart
is a key who's value is a list. Lists are sequential and are indexed with integers (eg, 0, 1, 2, ...).In your expression, you are trying perform a look up on the list using a string (value). This is where your
TypeError
is coming from.Other elements in the data such as this..