The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

Python Loop Response

Highlighted
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
Highlighted
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
Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured