From JSON to CSV

While I was working on creating a way to transfer the data to a more suitable storage site, I still needed to store some data so that I could examine it and test it on Arcmap. So I had to write a script that would take only the specific JSON that I wanted, then wrote it into a CSV from a Dictionary. To do this I had to write all of the header field names from the JSON which would allow me to load the rows.

#Use request.get().json() to call api in json format
waze = requests.get('waze_url').json()

#separate alerts
alerts = waze['alerts']
seen_alert_ids = set()

#Write to CSV using DictWriter
#In the beginning use 'wb+' for subsequent additions to csv use 'wb'
with open('filename.csv', 'wb') as csvfile_alerts:
fieldnames_alerts =['city', 'confidence', 'nThumbsUp', 'uuid', 'country', 'subtype', 'roadType',
'street', 'reliability', 'x', 'y', 'magvar', 'type', 'reportRating', 'pubMillis', 'reportDescription']
writer_alerts = csv.DictWriter(csvfile_alerts, fieldnames=fieldnames_alerts)

writer_alerts.writeheader()

The only problem I ran into was when I wrote the json to a CSV, the file did not separate x and y values. In order to fix this I simply separated out the dictionary of x and y from the ‘locations’ field, which gave me two new fields for the x coordinate and y coordinate.

#Create separate x and y fields from 'location' to replace single locations column

for a in alerts:
if a['uuid'] not in seen_alert_ids:
a['y'] = a['location']['y']
a['x'] = a['location']['x']
del a['location']

writer_alerts.writerow(a)
seen_alert_ids.add(a['uuid'])