Syntax error when calling "from" attribute of a JSON object

444 Views Asked by At

I am using RingCentral API to get call log information.

I am interested in an attribute called from which tells us who was the caller who made the call. To access the caller name, I need to access a call record -> then go to the last leg (root of the call) and then get the from attribute, and then get the name of the caller.

E.g:

"from": { "name": "andy murray", "phoneNumber": "+44712345656" }

resp = platform.get('/restapi/v1.0/account/~/call-log', params)       
for record in resp.json().records:
    print(record.legs[-1].from.name)

After running this command, I am getting error:

      File "demo.py", line 43
        print(record.legs[-1].from.name )
SyntaxError: invalid syntax

It looks like Python is not able to comprehend that this "from" is not part of Python itself. How can I define Python to consider this as an attribute of a JSON object?

Any help will be appreciated!

1

There are 1 best solutions below

0
Hassaan Anwar On

Use __dict__ to get all attributes of object. https://stackoverflow.com/a/39392891/9024042

As mentioned in the answer of the attached post. I called record.legs[-1].__dict__ and found out all attributes of json object.

I found that, there was no attribute named "from", It was "from_".

I hope it helps someone with the same issue