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}