question

Kevin Peters avatar image
Kevin Peters asked Edward Jung answered

Python SDK Iterate json()

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?


sdk
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered

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

print(resp.text())
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Kevin Peters avatar image
Kevin Peters answered Phong Vu commented

{

"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"

}

}

}


1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image Phong Vu ♦♦ commented ·

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.

1 Like 1 ·
Kevin Peters avatar image
Kevin Peters answered

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"])

1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Edward Jung avatar image
Edward Jung answered

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"]}')

1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys