cancel
Showing results for 
Search instead for 
Did you mean: 

Python Loop Response

Solution Provider

Python Loop Response

I'm new to python and the CC API v2 and I'm having some difficulties looping through / accessing returned JSON data.  For example...

 

rs = Result_Set(ccx.get_contacts(limit = 1, email = 'hpotter66@owlmail.com')).get_item(0)
for key in rs:
    print (" - r: ", key)
    for x in rs[key]:
        print ("  - x: ", x)
 
Output:
 - r:  id
  - x:  1
  - x:  4
  - x:  9
  - x:  8
  - x:  4
  - x:  9
  - x:  6
  - x:  8
  - x:  5
  - x:  3
 - r:  status
  - x:  A
  - x:  C
  - x:  T
  - x:  I
  - x:  V
  - x:  E
 - r:  fax
 - r:  addresses
 - r:  notes
 - r:  confirmed
Traceback (most recent call last):
  File "C:\py\t4_get_contacts.py", line 42, in <module>
    for x in rs[key]:
TypeError: 'bool' object is not iterable

 

So clearly, I'm doing something wrong here.

 

Anyone have any sample code for handling the return data?

 

Thanks,

Paul ---

 

1 REPLY 1
Moderator

Re: Python Loop Response

Hello @PaulR528,

 

Thank you for reaching out to Constant Contact's API Support.

 

I'm not an expert with python, but it looks like your for loop is iterating each line in the result as a string instead of as JSON data. Python supports JSON natively so you should be able to just call in something to do this for you.

 

I did a quick internet search and found a few resources. I personally liked the Hitchhikers guide to Python, but I may be biased to that reference.


Regards,
Jimmy D.
Tier II API Support Engineer