title: Handle a KeyError while parsing JSON
body: <p>Beginner question.</p>
  
  <p>I am curling my database to see if an object exists by a certain <code>requestId</code>. </p>
  
  <p>Ideally, my code would be something like:</p>
  
  <pre><code>if totalobjects_start == 0:
      create a cron job
  elif totalobjects_start &lt; 24:
      pass
  </code></pre>
  
  <p>This is fine when 1+ objects exist in the table with that requestId. Notice the property <code>totalObjects</code> is <code>1</code>.</p>
  
  <pre><code>{"offset":0,"data":[{"created":1439328152000,"locationId":0,"requestId":"A6NMD6","latency":0,"___class":"Latency","totalObjects":1}
  </code></pre>
  
  <p>However, when there doesn't exist an object in table with that <code>requestId</code>, the JSON dump looks like:</p>
  
  <pre><code>{"code":1017,"message":"Invalid where clause. Not existing columns: "}
  </code></pre>
  
  <p>What I am having trouble with is what to assign the variable for the conditional statement. If there is no object in the table and I try to assign <code>totalobjects_start</code> to the JSON property <code>totalObjects</code>, I will get a <code>KeyError</code>, since it doesn't exist in the JSON dump.</p>
  
  <p>Here is the pseudocode for what I am trying to do:</p>
  
  <pre><code>try to see if code 1017
  if not, see if totalObjects is at least 1
  
  if code 1017 (no objects):
      create a cron job
  elif totalObjects is at least 1 and less than 24:
      pass
  </code></pre>
  
  <p>The following code is obviously wrong, but I wanted to at least make an effort:</p>
  
  <pre><code>totalobjects_start_str = totalobjects_search_start.decode(encoding='utf-8')
  
  def totalobjects_count():
      try:
          totalobjects_start_1017 = json.loads(totalobjects_start_str, strict=False)['code']
      except (ValueError, KeyError, TypeError):
          totalobjects_start = int(json.loads(totalobjects_start_str, strict=False)['totalObjects'])
  
  totalobjects_count()
  
  if totalobjects_start_1017 == '1017':
      job.hours.every(2)
      cron.write()
  elif totalobjects_start &lt; 24:
      pass
  </code></pre>