JSON sub for loop produces KeyError, but key exists
<p>I'm trying to add the JSON output below into a dictionary, to be saved into a SQL database.</p>

<pre><code>{'Parkirisca': [
    {    
    'ID_Parkirisca': 2,
    'zasedenost': {
                   'Cas': '2016-10-08 13:17:00',
                   'Cas_timestamp': 1475925420,
                   'ID_ParkiriscaNC': 9,
                   'P_kratkotrajniki': 350
                  }
    }
]}
</code></pre>

<p>I am currently using the following code to add the value to a dictionary:</p>

<pre><code>import scraperwiki
import json
import requests
import datetime
import time
from pprint import pprint

html = requests.get("http://opendata.si/promet/parkirisca/lpt/")

data = json.loads(html.text)

for carpark in data['Parkirisca']:

    zas = carpark['zasedenost']
    free_spaces = zas.get('P_kratkotrajniki')
    last_updated = zas.get('Cas_timestamp')

    parking_type = carpark.get('ID_Parkirisca')
    if parking_type == "Avtomatizirano":
        is_automatic = "Yes"
    else:
        is_automatic = "No"

    scraped = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')

    savetodb = {
    'scraped': scraped,
    'id': carpark.get("ID_Parkirisca"),
    'total_spaces': carpark.get("St_mest"),
    'free_spaces': free_spaces,
    'last_updated': last_updated,
    'is_automatic': is_automatic,
    'lon': carpark.get("KoordinataX_wgs"), 
    'lat': carpark.get("KoordinataY_wgs")
    }

    unique_keys = ['id']

    pprint savetodb
</code></pre>

<p>However when I run this, it gets stuck at <code>for zas in carpark["zasedenost"]</code> and outputs the following error:</p>

<pre><code>Traceback (most recent call last):
File "./code/scraper", line 17, in &lt;module&gt;
    for zas in carpark["zasedenost"]:
KeyError: 'zasedenost'
</code></pre>

<p>I've been led to believe that <code>zas</code> is in fact now a string, rather than a dictionary, but I'm new to Python and JSON, so don't know what to search for to get a solution. I've also searched here on Stack Overflow for <code>KeyErrror when key exist</code> questions, but they didn't help, and I believe that this might be due to the fact that's a sub for loop.</p>

<p>Update: Now, when I swapped the double quotes for single quotes, I get the following error:</p>

<pre><code>Traceback (most recent call last):
File "./code/scraper", line 17, in &lt;module&gt;
    free_spaces = zas.get('P_kratkotrajniki')
AttributeError: 'unicode' object has no attribute 'get'
</code></pre>

<p>I fixed up your code:</p>

<ol>
<li>Added required imports.</li>
<li>Fixed the <code>pprint savetodb</code> line which isn't valid Python.</li>
<li>Didn't try to iterate over <code>carpark['zasedenost']</code>.</li>
</ol>

<p>I then added another <code>pprint</code> statement in the <code>for</code> loop to see what's in <code>carpark</code> when the <code>KeyError</code> occurs. From there, the error is clear. (Not all the elements in the array in your JSON contain the <code>'zasedenost'</code> key.)</p>

<p>Here's the code I used:</p>

<pre><code>import datetime
import json
from pprint import pprint
import time

import requests

html = requests.get("http://opendata.si/promet/parkirisca/lpt/")

data = json.loads(html.text)

for carpark in data['Parkirisca']:
    pprint(carpark)
    zas = carpark['zasedenost']
    free_spaces = zas.get('P_kratkotrajniki')
    last_updated = zas.get('Cas_timestamp')

    parking_type = carpark.get('ID_Parkirisca')
    if parking_type == "Avtomatizirano":
        is_automatic = "Yes"
    else:
        is_automatic = "No"

    scraped = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')

    savetodb = {
        'scraped': scraped,
        'id': carpark.get("ID_Parkirisca"),
        'total_spaces': carpark.get("St_mest"),
        'free_spaces': free_spaces,
        'last_updated': last_updated,
        'is_automatic': is_automatic,
        'lon': carpark.get("KoordinataX_wgs"), 
        'lat': carpark.get("KoordinataY_wgs")
    }

    unique_keys = ['id']

    pprint(savetodb)
</code></pre>

<p>And here's the output on the iteration where the <code>KeyError</code> occurs:</p>

<pre><code>{u'A_St_Mest': None,
 u'Cena_dan_Eur': None,
 u'Cena_mesecna_Eur': None,
 u'Cena_splosno': None,
 u'Cena_ura_Eur': None,
 u'ID_Parkirisca': 7,
 u'ID_ParkiriscaNC': 72,
 u'Ime': u'P+R Studenec',
 u'Invalidi_St_mest': 9,
 u'KoordinataX': 466947,
 u'KoordinataX_wgs': 14.567929171694901,
 u'KoordinataY': 101247,
 u'KoordinataY_wgs': 46.05457609543313,
 u'Opis': u'2,40 \u20ac /dan',
 u'St_mest': 187,
 u'Tip_parkirisca': None,
 u'U_delovnik': u'24 ur (ponedeljek - petek)',
 u'U_sobota': None,
 u'U_splosno': None,
 u'Upravljalec': u'JP LPT d.o.o.'}
Traceback (most recent call last):
  File "test.py", line 14, in &lt;module&gt;
    zas = carpark['zasedenost']
KeyError: 'zasedenost'
</code></pre>

<p>As you can see, the error is quite accurate. There's no key <code>'zasedenost'</code> in the dictionary. If you look through your JSON, you'll see that's true for a number of the elements in that array.</p>

<p>I'd suggest a fix, but I don't know what you want to do in the case where this dictionary key is absent. Perhaps you want something like this:</p>

<pre><code>zas = carpark.get('zasedenost')
if zas is not None:
    free_spaces = zas.get('P_kratkotrajniki')
    last_updated = zas.get('Cas_timestamp')
else:
    free_spaces = None
    last_updated = None
</code></pre>

python json list for-loop dictionary