TypeError: string indices must be integers while parsing a json

277 Views Asked by At

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?

2

There are 2 best solutions below

0
On

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..

{
    "caloriesOut": 740.15264,
    "max": 94,
    "min": 30,
    "minutes": 593,
    "name": "Out of Range"
}

Are dictionaries which allow you to use any hashable values for the key. This is why you're able to use a string for the key.


https://www.geeksforgeeks.org/difference-between-list-and-dictionary-in-python
0
On

See, let's store your data in a variable a, then,

When you are doing this, a['activities-heart']:

You will get an output like this:

[{'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}}]

This is a list and at 0 index is your value, therefore,

a['activities-heart'][0] will give you a dictionary.

Therefore you can perform like this:

a['activities-heart'][0]['value']['heartRateZones'][1]['minutes']

Output:

46