Search code examples
pythonjsontypeerrorfile-conversion

Why am I getting TypeError on code that worked previously?


I have this code to iterate through a json file. The user specifies tiers to be extracted, the names of which are then saved in inputLabels, and this for loop extracts the data from those tiers:

with open(inputfilename, 'r', encoding='utf8', newline='\r\n') as f:
        data = json.load(f)
        for line in data:
            if line['label'] in inputLabels:
                elements = [(e['body']['value']).replace(" ", "_") + "\t" for e in line['first']['items']]
                outputData.append(elements)

I wrote this code a year ago and have run it multiple times since then with no issues, but running it today I received a TypeError.

    if line['label'] in inputLabels:
TypeError: string indices must be integers

I don't understand why my code was able to work before if this is a true TypeError. Why is this only a problem in the code now, and how can I fix it?

EDIT: Pasted part of the json:

{
  "contains": [
    {
      "total": 118,
      "generated": "ELAN Multimedia Annotator 6.2",
      "id": "xxx",
      "label": "BAR001_TEXT",
      "type": "AnnotationCollection",
      "@context": "http://www.w3.org/ns/ldp.jsonld",
      "first": {
        "startIndex": "0",
        "id": "xxx",
        "type": "AnnotationPage",
        "items": [
          {
            "id": "xxx",
            "type": "Annotation",
            "body": {
              "purpose": "transcribing",
              "format": "text/plain",
              "language": "",
              "type": "TextualBody",
              "value": ""
            },
        "@context": "http://www.w3.org/ns/anno.jsonld",
        "target": {
          "format": "audio/x-wav",
          "id": "xxx",
          "type": "Audio"
        }
      },
      {
        "id": "xxx",
        "type": "Annotation",
        "body": {
          "purpose": "transcribing",
          "format": "text/plain",
          "language": "",
          "type": "TextualBody",
          "value": "Dobar vam"
        },
        "@context": "http://www.w3.org/ns/anno.jsonld",
        "target": {
          "format": "audio/x-wav",
          "id": "xxx",
          "type": "Audio"
        }
      },
      {
        "id": "xxx",
        "type": "Annotation",
        "body": {
          "purpose": "transcribing",
          "format": "text/plain",
          "language": "",
          "type": "TextualBody",
          "value": "Je"
        },
        "@context": "http://www.w3.org/ns/anno.jsonld",
        "target": {
          "format": "audio/x-wav",
          "id": "xxx",
          "type": "Audio"
        }
      },

Solution

  • Your code would probably work if you replaced for line in data: with for line in data['contains']

    Maybe the JSON schema didn't have the "contains" level previously.