I'm trying to read a JSON file (and eventually save the contents to a database), but I'm getting an KeyError when running the code below. See sample JSON for reference.
The keys 'durationTimeFrom'
and 'durationTimeTo'
does not exist in all cases in the JSON file (only when eventType
is 80
, I think).
How do I properly identify missing keys before attempting to read the value, and/or how do I insert dummy-keys for the items that do not have the key already?
I have already searched Google/StackOverflow and tried if key in dict:
and dict.get(key)
without success.
import json
source = "feed_traffic.json" # JSON file to process
data = json.loads(open(source).read()) # Process JSON
total = data["events"]["totalCount"]
index = 0
events = data["events"]["list"][index:total]
for event in events:
eid = event["id"]
type = event['eventType']
header = event['headingText']
lat = event['latitude']
long = event['longitude']
created = event['created']
updated = event['updated']
expires = event['expireTime']
validFrom = event['durationTimeFrom']
validTo = event['durationTimeTo']
status = event['status']
# Just to check all is well, out data in console
print("Traffic Event", index, ":", eid, "-", type, "-", header, "(", long, ",", lat, ")")
index = index+1
dict.get
. it works fine for me. You should change as follows.
# before
validFrom = event['durationTimeFrom']
validTo = event['durationTimeTo']
# after
validFrom = event.get('durationTimeFrom')
validTo = event.get('durationTimeTo')