get objectId from entries returned from search result [Python]

I am retrieving a list of entries in the Request table with the requestId equal to 1. At the moment, there is only one entry with that value. Then I am converting the results to a string then grabbing the objectId.

However, I get the error "KeyError: ‘objectId’


new_requests = Popen(['curl',
                      '-H', 'application-id: %s' % backendless_appid,
                      '-H', 'secret-key: %s' % backendless_sk,
                      '-H', 'Content-Type: application/json',
                      '-X', 'GET',
                      '-v', 'https://api.backendless.com/v1/data/Request?where=requestId%3D1'],
                     stdout=PIPE).communicate()[0]


new_requests_str = new_requests.decode(encoding='utf-8')


new_requests_objectid = json.loads(new_requests_str, strict=False)['objectId']

Printing new_requests_str returns nothing, but I don’t understand why.

Could you try running that curl request from a command line and post here the response?

The curl returns the JSON results just fine. New findings: new_requests and new_requests_str both return JSON results, but the last part, new_requests_objectid, can’t get the objectId.

Sorry, I am not familiar with Python. If the backend returns the value, then you should be able to retrieve it.

Yeah it seems like it isn’t a Backendless problem anymore, but Python. I’ll keep digging. Thanks for the help.

Solution:

The problem was that objectId is not a direct key in the JSON. It was part of a dictionary in the “data” list.


new_requests_objectIds = []


for data_item in json.loads(new_requests_str, strict=False)['data']:
    new_requests_objectIds.append(data_item['objectId'])

I am glad you solved it!