0

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
4
  • have you tried implementing error and exception handling? docs.python.org/2/tutorial/errors.html Commented Mar 22, 2017 at 16:31
  • event.get('id') returns None if the key doesn't exist Commented Mar 22, 2017 at 16:32
  • help(dict.get) says D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None. You can provide your own default. Supposing you want the default id to be -1, you would eid = event.get("id", -1). Commented Mar 22, 2017 at 16:36
  • @glls Yeah, that is my next step ;-) Thx for the input Commented May 23, 2017 at 11:41

1 Answer 1

2

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I don't know what I did wrong the first time, but I was getting the same error. This sorted it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.