I'm attempting to convert a .json file to a .csv so that I can perform analysis in R. I'm following steps suggested by others, but still run into problems (likely due to the large size of the json file). First, I pull the URL from the web:
import urllib
#first open html as .json
response = urllib.request.urlopen("http://trends.vera.org/data/county/1003/")
input = response.read()
print(input)
This function below I got from the linked question to flatten the json file.
#function to flatten .json file
def flattenjson( b, delim ):
val = {}
for i in b.keys():
if isinstance( b[i], dict ):
get = flattenjson( b[i], delim )
for j in get.keys():
val[ i + delim + j ] = get[j]
else:
val[i] = b[i]
return val
The lines below take in a list and generate column names for the csv. This is where something is going wrong. Does anyone know how to fix this?
#find column names
input = map( lambda x: flattenjson( x ), input )
columns = map( lambda x: x.keys(), input )
columns = reduce( lambda x,y: x+y, columns )
columns = list( set( columns ) )
print(columns)
Finally, I write the json data to a .csv file.
#write to .csv file
with open( fname, 'wb' ) as out_file:
csv_w = csv.writer( out_file )
csv_w.writerow( columns )
for i_r in input:
csv_w.writerow( map( lambda x: i_r.get( x, "" ), columns ) )
Thanks in advance for any help.