JSON and Python - flatten JSON api for CSV with TypeError: list indices must be integers, not str
Question: Trying to flatten JSON-formatted data rec'd via API with a somewhat
unpredictable hierarchy with the goal of saving it as a CSV for import into
MySQL and I'm running into an error **_TypeError: list indices must be
integers, not str_**

  * data1 is a list with the results of the API call ...

CODE:

    
    
        json_data1 = json.dumps(data1)
    
    ## Flatten JSON
    
    from collections import OrderedDict
    import csv
    
    outfile = open("output.csv", "w")
    
    writer = csv.writer(outfile, delimiter=",")
    
    data = json.loads(json_data1, object_pairs_hook=OrderedDict)
    
    # Recursively flatten JSON
    def flatten(structure, key="", path="", flattened=None): 
        if flattened is None:
            flattened = OrderedDict()    
        if type(structure) not in(OrderedDict, list):
            flattened[((path + "_") if path else "") + key] = structure    
        elif isinstance(structure, list):
            for i, item in enumerate(structure):
                flatten(item, "", path + "_" + key, flattened)    
        else:
            for new_key, value in structure.items():
                flatten(value, new_key, path + "_" + key, flattened)    
        return flattened
    
    # Write fields
    fields = []
    for result in data["results"]:
        flattened = flatten(data["results"][0])
        for k, v in flattened.iteritems():
            if k not in fields:
                fields.append(k)
    writer.writerow(fields)
    
    # Write values
    for result in data["results"]:
        flattened = flatten(result)
        row = []
        for field in fields:
            if field in flattened.iterkeys():
                row.append(flattened[field])
            else:
                row.append("")
        writer.writerow(row)
    

Any ideas on where the error is?


Answer: I think you'll need to post more output for someone to troubleshoot this. What
line number is the error on?

In general this error means you're trying to pass a non integer value in a
list. For example, if you have list:

list = ["a", "b", "c"]

You can do list[0] to get "a". If you do list["pig"] you get "TypeError: list
indices must be integers, not str".

Just add some print statements before your lists and you should be able to
spot this.

