Sunday, 17 January 2021

Populating JSON data from API in Python pandas DataFrame - TypeError and IndexError

I am trying to populate a pandas DataFrame with select information from JSON output fetched from an API.

candidate_list = []

for candidate in candidate_response['data']:
    if 'error' not in candidate_response:
       candidate_list.append([candidate['id'], candidate['attributes']['first_name'], candidate['attributes']
       ['last_name'], candidate['relationships']['educations']['data']['id']])

The DataFrame populates fine untill I add candidate['relationships']['educations']['data']['id'], which throws TypeError: list indices must be integers or slices, not str.

When trying to get the values of the indexes for ['id'] by using candidate['relationships']['educations']['data'][0]['id'] instead, I get IndexError: list index out of range.

The JSON output looks something like:

"data": [
    {
        "attributes": {
            "first_name": "Tester",
            "last_name": "Testman",
            "other stuff": "stuff",
        },
        "id": "732887",
        "relationships": {
            "educations": {
                "data": [
                    {
                        "id": "605372",
                        "type": "educations"
                    },
                    {
                        "id": "605371",
                        "type": "educations"
                    },
                    {
                        "id": "605370",
                        "type": "educations"
                    }
                ]
            }
        },

How would I go about sucessfully filling a column in the DataFrame with the 'id's under 'relationships'>'educations'>'data'?

Appreciate any thoughts.



from Populating JSON data from API in Python pandas DataFrame - TypeError and IndexError

No comments:

Post a Comment