News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Python SDK Iterate json()
Tags: sdk
Jul 13, 2021 at 7:50am   •   4 replies  •  0 likes
Kevin Peters

Your example iterates the the json at the top level like this:

for record in resp.json().records:

print "Call type: " + record.type

I am getting an error that the object is not iterable if I try and iterate a nested key with this:

for callfrom in record.from:

How do I iterate the nested keys?


4 Answers
answered on Mar 20, 2024 at 8:17am  

Issue is reserved word "from". Use standard python json object instead of the ringcentral JsonObject as indicated in your answer but you can do this instead:

response = json.loads(resp.text())

for record in response["records"]:
print(f'Call result: {record["result"]} {record["startTime"]} from {record["from"]["phoneNumber"]}')


 0
answered on Jul 14, 2021 at 11:02am  

I figured it out a solution. I can traverse the call log data and get the to and from phone numbers like this:

logdata=resp.text()

dict1 = json.loads(logdata)

for x, y in dict1.items():

if x == 'records':

for item in y:

if isinstance(item,dict):

for a, b in item.items():

if a == 'to':

print('To',b["phoneNumber"])

if a == 'from':

print('From',b["phoneNumber"])


 0
answered on Jul 13, 2021 at 8:03am  

What API endpoint response was that? Can you print the entire response like this

print(resp.text())

 0
answered on Jul 13, 2021 at 8:07am  

{

"uri" : "https://platform.devtest.ringcentral.com/restapi/v1.0/account/299164004/call-log?view=Simple&showBlocked=true&withRecording=false&dateFrom=2010-06-01T00:00:00.000Z&page=1&perPage=100",

"records" : [ {

"uri" : "https://platform.devtest.ringcentral.com/restapi/v1.0/account/299164004/call-log/AiQdXSahI0gKzUA?view=Simple",

"id" : "AiQdXSahI0gKzUA",

"sessionId" : "25883985004",

"startTime" : "2021-07-12T18:50:00.581Z",

"duration" : 31,

"type" : "Voice",

"internalType" : "LocalNumber",

"direction" : "Inbound",

"action" : "Phone Call",

"result" : "Missed",

"to" : {

"phoneNumber" : "+14242728375"

},

"from" : {

"phoneNumber" : "+18604281711",

"location" : "Willimantic, CT"

},

"telephonySessionId" : "s-65ac02cc65d84ee195e1fe42d2b0ed24"

}, {

"uri" : "https://platform.devtest.ringcentral.com/restapi/v1.0/account/299164004/call-log/AiQdW8NbquqAzUA?view=Simple",

"id" : "AiQdW8NbquqAzUA",

"sessionId" : "25883984004",

"startTime" : "2021-07-12T18:50:00.489Z",

"duration" : 31,

"type" : "Voice",

"internalType" : "LongDistance",

"direction" : "Outbound",

"action" : "RingOut PC",

"result" : "Call connected",

"to" : {

"name" : "Jay Peters",

"phoneNumber" : "+14242728375",

"location" : "Santa Monica Santa Monica, CA"

},

"from" : {

"name" : "Jay Peters",

"phoneNumber" : "+18604281711",

"extensionId" : "299164004"

},

"extension" : {

"uri" : "https://platform.devtest.ringcentral.com/restapi/v1.0/account/299164004/extension/299164004",

"id" : 299164004

},

"telephonySessionId" : "s-9d40efdebaf04bae81b2a8859cef6adf"

} ],

"paging" : {

"page" : 1,

"perPage" : 100,

"pageStart" : 0,

"pageEnd" : 1

},

"navigation" : {

"firstPage" : {

"uri" : "https://platform.devtest.ringcentral.com/restapi/v1.0/account/299164004/call-log?view=Simple&showBlocked=true&withRecording=false&dateFrom=2010-06-01T00:00:00.000Z&page=1&perPage=100"

}

}

}



 0
on Jul 13, 2021 at 10:04am   •  1 likes

The response is valid and the resp.json().records should be a valid array too. Not sure why you got that error. Can you try with Python 3.x? It shouldn't be a matter but just a test.



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us