jones
jones

Reputation: 361

Currently Recieving a Key Error for JSON data retrieved from API in Python. What is going on?

I am currently recieving a "Key Error" in Python, I have no idea what is going on.

Here is my code:

""" This is a program from http://www.masnun.me/2010/01/30/handling-json-in-   python.html which I am
using to experiment with how to handle JSON data in python, currently the problem is that I have a bunch
of JSON data returned from the nestoria API, and I don't know how to get what I want out of it.
"""

import json,urllib
data = urllib.urlopen("http://api.nestoria.com.au/api?country=au&pretty=1&action=search_listings&encoding=json&listing_type=rent&centre_point=-33.8891,151.1870,3km&number_of_results=30&sort=bedroom_highlow&page=1").read()
d = json.loads(data)
for x in d['response']['listings']:
#check that the necessary data fields we are retriving are not empty and check that  the number of bedrooms is within a reasonable range so we do not divide by zero's or catch typo's
    if ((int(x['bedroom_number'])!=0)and x['title']!=None and x['latitude'] !=None and x['longitude'] !=None and x['price_high'] !=None):
        print x['title'], x['latitude'], x['longitude'], x['bedroom_number'], x['price_high'], 'The price per bedroom is... ', (x['price_high'])/int((x['bedroom_number']))

    #note currently getting a python "key error"

Here is the output:

Flat for rent, Broadway - Lift -33.88440 151.19524 15 100 The price per bedroom is...  6
Riley Street, Surry Hills - Terrace -33.88462 151.21254 6 2000 The price per bedroom is...  333
Quay Street, Haymarket - Patio, Lift -33.88184 151.20316 6 2094 The price per bedroom is...  349
Bourke Street, Darlinghurst - Terrace -33.87921 151.21712 5 1950 The price per bedroom is...  390
Wigram Road, Glebe -33.87953 151.18179 5 900 The price per bedroom is...  180
House for rent, Newtown -33.89859 151.17581 5 0 The price per bedroom is...  0
House to let, Marrickville - Backyard -33.91251 151.16055 5 1200 The price per bedroom is...  240
Warren Ball Avenue Newtown -33.89426 151.18604 5 2000 The price per bedroom is...  400
Darling Island Road, Darling Island -33.86879 151.19421 4 2500 The price per bedroom is...  625
Wellington St, Waterloo - Backyard -33.89860 151.20753 4 850 The price per bedroom is...  212
Cathedral Street, Woolloomooloo -33.87222 151.21709 4 1000 The price per bedroom is...  250
Harold Street, Newtown -33.90095 151.18114 4 750 The price per bedroom is...  187
Jarocin Avenue, Glebe - Terrace -33.88185 151.18430 4 750 The price per bedroom is...  187
Talfourd Street, Glebe - Fireplace -33.87892 151.18727 4 1200 The price per bedroom is...  300
Douglas Street Stanmore - Backyard -33.89336 151.16078 4 730 The price per bedroom is...  182

Traceback (most recent call last):
  File "C:\Users\turing\Documents\Programming Experiments\python\handlingjsonpython.py", line 13, in <module>
        if ((int(x['bedroom_number'])!=0)and x['title']!=None and x['latitude'] !="" and x['longitude'] !=None and x['price_high'] !=None):
KeyError: 'latitude'

Any help with how to fix this "key error" would be greatly appreciated. I am completely new to handling JSON in Python.

Upvotes: 1

Views: 3077

Answers (1)

John Machin
John Machin

Reputation: 83022

Don't use d.has_key(k); it's slow and deprecated. Use k in d.

Don't compare == None or != None, use is None or is not None.

Don't waste keystrokes on d.get(k, None) -- None is the default. Use d.get(k)

So, use ... and x.get('latitude') is not None and ...

Update running that query just now, I noticed that two of the results had neither latitude nor longitude.

I'd suggest that you break that big if statement down into bite-size chunks, and avoid the horrid x['attribute_name'] way of referring to data:

bedroom_number = int(x.get('bedroom_number', '0'))
latitude = x.get('latitude')
longitude = x.get('longitude')
title = x.get('title')
price_high = x.get('price_high')
if not (bedroom_number and latitude and longitude and title and price_high): continue
print title, latitude, longitude, bedroom_number, price_high, \
    'The price per bedroom is... ', float(price_high) / bedroom_number

Upvotes: 6

Related Questions