Oli Oli - 1 year ago 94
Python Question

OOP - Accessing Object Data

Apologies if this is a repeated or simple question.

I have recently been learning Python (having spent many years writing simple MATLAB scripts). I've started exploring Object Oriented Programming and JSON.

I am trying to use an API to collect data from a server. When the objects are returned I'm mostly doing fine with using syntax to access particular data fields. However, I'm struggling with one. I have a row object:

row = {"totalCount": 1, "results": [{"parentObjectId": 887, "contextData": ["Row 1"], "parentObjectType": "sheet", "objectId": 599, "text": "Text", "parentObjectName": "Data", "objectType": "row"}]}

I am trying to access the "objectId" attribute for the single result (

I have tried
rowId = row.results[0].objectId
but get the error "'SearchResultItem' object has no attribute 'objectId'".

I have also tried
rowId = row.results[0]['objectId']
but get the error "'SearchResultItem' object has no attribute '

--- EDIT:

Traceback (most recent call last):

File "<ipython-input-46-14e026c273e3>", line 1, in <module>

TypeError: 'SearchResultItem' object has no attribute '__getitem__'

I am using a tool called Smartsheet. I am using the search_sheet request. The API documentation (http://smartsheet-platform.github.io/api-docs/#search-sheet) says that 'SearchResultItem' is an object containing a number of attributes. It doesn't give much more information.

The Smartsheet models are found here: https://github.com/smartsheet-platform/smartsheet-python-sdk/tree/master/smartsheet/models. I am currently looking at search_result.py and search_result_item.py to find the answer/clues.


Thanks for any help!

Answer Source

Your library code clearly says, that SearchResultItem has a property .object_id.

print(reportingRow.results[0].object_id)  # this works just fine

Your problem is not dictionary/JSON-related, because you are not using dictionaties. You are using custom objects wrapped around those dictionaries.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download