Home > Enterprise >  TypeError returned when trying to parse REST API JSON into CSV in Python
TypeError returned when trying to parse REST API JSON into CSV in Python

Time:10-14

This is the JSON structure I'm trying to parse into a table, then into a CSV:

{
    "id": 1,
    "publisher": {
        "id": 1,
        "name": "Marvel"
    },
    "series": {
        "id": 1,
        "name": "Death of the Inhumans",
        "sort_name": "Death of the Inhumans",
        "volume": 1,
        "series_type": {
            "id": 3,
            "name": "Mini-Series"
        },
        "genres": []
    },
    "number": "1",
    "title": "",
    "name": [
        "Chapter One: Vox"
    ],
    "cover_date": "2018-09-01",
    "store_date": "2018-07-04",
    "price": "4.99",
    "rating": {
        "id": 1,
        "name": "Unknown"
    },
    "sku": "",
    "isbn": "",
    "upc": "75960608996300111",
    "page": 40,
    "desc": "THE TITLE SAYS IT ALL - HERE LIE THE INHUMANS.\r\n\r\nThe Kree have gone murdering, leaving behind a message: Join or die. Thousands of Inhumans have already made their choice - the evidence floats bleeding in space. Black Bolt and his family are next. Rising star Donny Cates and PUNISHER: WAR JOURNAL artist Ariel Olivetti bring their brutal talents to the Inhumans!",
    "image": "https://static.metron.cloud/media/issue/2018/11/11/6497376-01.jpg",
    "arcs": [],
    "credits": [
        {
            "id": 6,
            "creator": "Ariel Olivetti",
            "role": [
                {
                    "id": 2,
                    "name": "Artist"
                },
                {
                    "id": 7,
                    "name": "Cover"
                }
            ]
        },
        {
            "id": 5,
            "creator": "Clayton Cowles",
            "role": [
                {
                    "id": 6,
                    "name": "Letterer"
                }
            ]
        },
        {
            "id": 1,
            "creator": "Donny Cates",
            "role": [
                {
                    "id": 1,
                    "name": "Writer"
                }
            ]
        },
        {
            "id": 9,
            "creator": "Greg Hildebrandt",
            "role": [
                {
                    "id": 7,
                    "name": "Cover"
                }
            ]
        },
        {
            "id": 11,
            "creator": "Javier Garrón",
            "role": [
                {
                    "id": 7,
                    "name": "Cover"
                }
            ]
        },
        {
            "id": 7,
            "creator": "Kaare Andrews",
            "role": [
                {
                    "id": 7,
                    "name": "Cover"
                }
            ]
        },
        {
            "id": 10,
            "creator": "Matthew Wilson",
            "role": [
                {
                    "id": 7,
                    "name": "Cover"
                }
            ]
        },
        {
            "id": 2,
            "creator": "Russell Dauterman",
            "role": [
                {
                    "id": 7,
                    "name": "Cover"
                }
            ]
        },
        {
            "id": 8,
            "creator": "Wil Moss",
            "role": [
                {
                    "id": 8,
                    "name": "Editor"
                }
            ]
        }
    ],
    "characters": [
        {
            "id": 1,
            "name": "Black Bolt",
            "modified": "2021-09-09T15:52:49.090281-04:00"
        },
        {
            "id": 5,
            "name": "Crystal",
            "modified": "2021-09-09T15:53:03.317155-04:00"
        },
        {
            "id": 3,
            "name": "Gorgon",
            "modified": "2021-09-09T15:53:30.726576-04:00"
        },
        {
            "id": 4,
            "name": "Karnak",
            "modified": "2021-09-09T15:53:54.283332-04:00"
        },
        {
            "id": 8,
            "name": "Lockjaw",
            "modified": "2019-06-23T15:13:20.510882-04:00"
        },
        {
            "id": 6,
            "name": "Maximus",
            "modified": "2021-09-09T15:54:35.424933-04:00"
        },
        {
            "id": 2,
            "name": "Medusa",
            "modified": "2021-09-09T15:54:16.254153-04:00"
        },
        {
            "id": 7,
            "name": "Triton",
            "modified": "2021-09-09T15:55:00.566831-04:00"
        },
        {
            "id": 9,
            "name": "Vox",
            "modified": "2019-06-23T15:13:21.315947-04:00"
        }
    ],
    "teams": [
        {
            "id": 1,
            "name": "Inhumans",
            "modified": "2019-06-23T15:13:23.975156-04:00"
        }
    ],
    "reprints": [],
    "variants": [
        {
            "name": "Second Printing Cover",
            "sku": "",
            "upc": "",
            "image": "https://static.metron.cloud/media/variants/2018/11/11/inhumans-01-2nd-print.jpg"
        },
        {
            "name": "Greg Hildebrandt Variant Cover",
            "sku": "",
            "upc": "",
            "image": "https://static.metron.cloud/media/variants/2018/11/11/inhuman-01-variant.jpg"
        },
        {
            "name": "Young Guns Variant Cover",
            "sku": "",
            "upc": "",
            "image": "https://static.metron.cloud/media/variants/2018/11/11/inhuman-01-garron.jpg"
        },
        {
            "name": "Young Guns Variant Cover",
            "sku": "",
            "upc": "",
            "image": "https://static.metron.cloud/media/variants/2018/11/11/inhuman-01-russell.jpg"
        },
        {
            "name": "Design Variant Cover",
            "sku": "",
            "upc": "",
            "image": "https://static.metron.cloud/media/variants/2019/07/17/death-inhumans-1z.webp"
        }
    ],
    "modified": "2022-05-26T16:24:03.608369-04:00"
}

It's from an open comic database and API https://metron.cloud/

The structure returns Type Errors when I try to parse the keys. I did notice that there is only curly braces around the structure and not brackets. I'm newer to this so there is probably an obvious answer. Here is my code:

r = requests.get(url, auth=auth)

issue_details = []

data = r.json()

for item in data:
    id = item['id']
    publisher = item['publisher']
    series = item['series']
    number = item['number']
    title = item['title']

    issue = {
        'id': id,
        'publisher': publisher,
        'series': series,
        'number': number,
        'title': title,
    }
    
    issue_details.append(issue)

print(issue_details)

And here is the error it throws:

Traceback (most recent call last):
  File "...\metron-exporter_test3.py", line 17, in <module>
    id = item['id']
TypeError: string indices must be integers

Am I selecting keys in the wrong way? or should I be handling this particular JSON structure differently?

CodePudding user response:

You don't need to iterate through the data since it's a dictionary. While iterating through the dictionary it will give the key value that's why it's raising a TypeError.

You can access value like this,

issue = {
        'id': data['id'],
        'publisher': data['publisher'],
        'series': data['series'],
        'number': data['number'],
        'title': data['title'],
    }

issue will be like this,

{'id': 1,
 'publisher': {'id': 1, 'name': 'Marvel'},
 'series': {'id': 1,
  'name': 'Death of the Inhumans',
  'sort_name': 'Death of the Inhumans',
  'volume': 1,
  'series_type': {'id': 3, 'name': 'Mini-Series'},
  'genres': []},
 'number': '1',
 'title': ''}
  • Related