Skip to main content
The First Step
July 13, 2021
Question

Python SDK Iterate json()

  • July 13, 2021
  • 5 replies
  • 743 views

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?


5 replies

PhongVu
Community Manager
Community Manager
July 13, 2021

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

print(resp.text())
The First Step
July 13, 2021

{

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

}

}

}


PhongVu
Community Manager
Community Manager
July 13, 2021

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.

The First Step
July 14, 2021

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

New Participant
March 20, 2024

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