0

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 :)

mkjohn
  • 3
  • 2
  • possible duplicate of [convert from json to csv using python](http://stackoverflow.com/questions/1871524/convert-from-json-to-csv-using-python) – scrappedcola Aug 19 '15 at 16:57

1 Answers1

0

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

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

cezaminio
  • 26
  • 2
  • I tried that but couldn't get it to work... I also tried using ipython to generate json file and then some python code to generate the .csv but haven't figured that out yet! – mkjohn Aug 19 '15 at 17:01
  • if you can code in php it's as simple as $data = json_decode(file_get_contents('json_url')); and then looping over the data and printing out what you need with csv delimiter – cezaminio Aug 19 '15 at 17:10
  • Not able to do that currently but I follow tutorials. I've amended the question above with some more detail – mkjohn Aug 19 '15 at 17:26