Reputation: 41
I am encountering a KeyError
when parsing JSON
results in Python. I've searched other questions relating to KeyError, but none of the answers has helped so far. When I make the web request
https://property.melissadata.net/v4/WEB/LookupProperty?id=MY_LICENSE_KEY&t=&cols=GrpPrimaryOwner&format=json&ff=5933 NE Garfield Avenue, Portland, OR, 97211
from a web browser, it returns the result
{"Version":"5.0.1.1043","TransmissionResults":"","TotalRecords":1,"Records":[{"Results":"YS02,YS07,YC01","Parcel":{"FIPSCode":"41051","UnformattedAPN":"R243379","FormattedAPN":"R243379"},"Legal":{},"PropertyAddress":{},"ParsedPropertyAddress":{},"PrimaryOwner":{"Name1Full":"PORTLAND PIEDMONT GUESTHOUSE L","Name1First":"","Name1Middle":"","Name1Last":"PORTLAND PIEDMONT GUESTHOUSE L"
...
The result of the Python code
import requests
import pprint
# api-endpoint
URL = “https://property.melissadata.net/v4/WEB/LookupProperty?id=MY_LICENSE_KEY&t=&cols=GrpPrimaryOwner&format=json”
# location given here
location = "5933 NE Garfield Avenue, Portland, OR 97211"
# defining a params dict for the parameters to be sent to the API
PARAMS = {'address':location}
# sending get request and saving the response as response object
r = requests.get(url = URL, params = PARAMS)
# extracting data in json format
data = r.json()
pprint.pprint(data)
name = data['Records'][0]['PrimaryOwner'][0]['Name1Full'][0]
print('name: ' + name)
is
{'Records': [{'Results': 'YE01'}],
'TotalRecords': 1,
'TransmissionResults': '',
'Version': '5.0.1.1043'}
>>> name = data['Records'][0]['PrimaryOwner'][0]['Name1Full'][0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'PrimaryOwner'
>>> print('name: ' + name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "list") to str
I would like to extract the Name1Full value of "PORTLAND PIEDMONT GUESTHOUSE L
" from the JSON results. I appreciate any help you can provide.
Solution:
This works when PARAMS = {'address':location}
is replaced with PARAMS = {'ff':location}
Upvotes: 0
Views: 1097
Reputation: 41
This works when PARAMS = {'address':location} is replaced with PARAMS = {'ff':location}
Upvotes: 0
Reputation: 101
I simplified the problem. You are not getting what you think you are getting back. This snippet below reproduces the problem, I have removed the extraneous code.
data = {'Records': [{'Results': 'YE01'}],
'TotalRecords': 1,
'TransmissionResults': '',
'Version': '5.0.1.1043'}
name = data['Records'][0]['PrimaryOwner'][0]['Name1Full'][0]
print('name: ' + name)
Upvotes: 1
Reputation: 297
The JSON you pretty print in the second part of your question doesn't have a 'PrimaryOwner'
field which is why you are getting a key error.
{'Records': [{'Results': 'YE01'}],
'TotalRecords': 1,
'TransmissionResults': '',
'Version': '5.0.1.1043'}
I think the question is: Why is your program getting different JSON data than your web browser? I can't help you further with that but perhaps someone else can.
Upvotes: 1
Reputation: 18367
Given the piece of json
you provide I believe you should try with:
full_name = data['Records'][0]['PrimaryOwner']['Name1Full']
Let's see how this work:
[Records][0]
access:
{"Results":"YS02,YS07,YC01","Parcel":{"FIPSCode":"41051","UnformattedAPN":"R243379","FormattedAPN":"R243379"},"Legal":{},"PropertyAddress":{},"ParsedPropertyAddress":{},"PrimaryOwner":{"Name1Full":"PORTLAND PIEDMONT GUESTHOUSE L","Name1First":"","Name1Middle":"","Name1Last":"PORTLAND PIEDMONT GUESTHOUSE L"...
We then need see that Name1Full
is a value for the PrimaryOwner
key, therefore [`Records'][0]['PrimaryOwner'] access:
{"Name1Full":"PORTLAND PIEDMONT GUESTHOUSE L","Name1First":"","Name1Middle":"","Name1Last":"PORTLAND PIEDMONT GUESTHOUSE L"...
Finally we add the key Name1Full
to access the specific value [`Records'][0]['PrimaryOwner']['Name1First']:
Returning:
"PORTLAND PIEDMONT GUESTHOUSE L"
Upvotes: 1