Converting JSON files to .csv

135 views Asked by At

I've found some data that someone is downloading into a JSON file (I think! - I'm a newb!). The file contains data on nearly 600 football players.

Here you can find the file

In the past, I have downloaded the json file and then used this code:

import csv
import json

json_data = open("file.json")
data = json.load(json_data)

f = csv.writer(open("fix_hists.csv","wb+"))

arr = []

for i in data:
    fh = data[i]["fixture_history"]
    array = fh["all"]
    for j in array:

        try:
            j.insert(0,str(data[i]["first_name"]))
        except:
            j.insert(0,'error')

        try:
            j.insert(1,data[i]["web_name"])
        except:
            j.insert(1,'error')

        try:
            f.writerow(j)
        except:
            f.writerow(['error','error'])

json_data.close()

Sadly, when I do this now in command prompt, i get the following error:

Traceback (most recent call last): File"fix_hist.py", line 12 (module) fh = data[i]["fixture_history"] TypeError: list indices must be integers, not str

Can this be fixed or is there another way I can grab some of the data and convert it to .csv? Specifically the 'Fixture History'? and then 'First'Name', 'type_name' etc.

Thanks in advance for any help :)

1

There are 1 answers

3
cezaminio On

Try this tool: http://www.convertcsv.com/json-to-csv.htm

You will need to configure a few things, but should be easy enough.