Converting NBA play by play specific .json to .csv
Question: I am trying to build a database containing play by play data for several
seasons of NBA games, for my Msc. in economics dissertation. Currently I am
extracting games from the NBA's API ([see
example](http://stats.nba.com/stats/playbyplayv2?GameID=0041300402&StartPeriod=0&EndPeriod=0&tabView=playbyplay))
and splitting each game into a different .json file using [this
routine](https://github.com/gmf05/nba/blob/master/scripts/py/savejson.py)
(duly adapted for p-b-p purposes), thus yielding .json files as (first play
example):

    
    
    {"headers": ["GAME_ID", "EVENTNUM", "EVENTMSGTYPE", "EVENTMSGACTIONTYPE", "PERIOD", "WCTIMESTRING", "PCTIMESTRING", "HOMEDESCRIPTION", "NEUTRALDESCRIPTION", "VISITORDESCRIPTION", "SCORE", "SCOREMARGIN"], "rowSet": [["0041400406", 0, 12, 0, 1, "9:11 PM", "12:00", null, null, null, null, null], ["0041400406", 1, 10, 0, 1, "9:11 PM", "12:00", "Jump Ball Mozgov vs. Green: Tip to Barnes", null, null, null, null]
    

I plan on **creating a loop to convert all of the generated .json files to
.csv** , such that it allows me to proceed to econometric analysis in stata.
At the moment, I am stuck in the first step of this procedure: the creation of
the json to CSV conversion process (I will design the loop afterwards). The
code I am trying is:

    
    
    f = open('pbp_0041400406.json') 
    data = json.load(f) 
    f.close()
    
    with open("pbp_0041400406.csv", "w") as file:
        csv_file = csv.writer(file)
    
        for rowSet in data:
            csv_file.writerow(rowSet)
    
    f.close()
    

However, the yielded CSV files are showing awkward results: one line reading
`h,e,a,d,e,r,s` and another reading `r,o,w,S,e,t`, thus not capturing the
headlines or rowSet(the plays themselves).

I have tried to solve this problem taking into account the contributes [on
this thread](http://stackoverflow.com/questions/1871524/how-can-i-convert-
json-to-csv-with-python?newreg=b44536fc4e274a0287105b853feec545), but I have
not been able to do it. Can anybody please provide me some insight into
solving this problem?

[EDIT] Replacing rowset with data in the original code also yielded the same
results.

Thanks in advance!


Answer: try this:

    
    
    import json
    import csv
    
    with open('json.json') as f:
        data = json.load(f)
    
    
    with open("pbp_0041400406.csv", "w") as fout:
        csv_file = csv.writer(fout, quotechar='"')
    
        csv_file.writerow(data['headers'])
    
        for rowSet in data['rowSet']:
            csv_file.writerow(rowSet)
    

Resulting CSV:

    
    
    GAME_ID,EVENTNUM,EVENTMSGTYPE,EVENTMSGACTIONTYPE,PERIOD,WCTIMESTRING,PCTIMESTRING,HOMEDESCRIPTION,NEUTRALDESCRIPTION,VISITORDESCRIPTION,SCORE,SCOREMARGIN
    
    0041400406,0,12,0,1,9:11 PM,12:00,,,,,
    
    0041400406,1,10,0,1,9:11 PM,12:00,Jump Ball Mozgov vs. Green: Tip to Barnes,,,,
    

