Python Loop Response

Occasional Participant

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 = '')).get_item(0)
for key in rs:
    print (" - r: ", key)
    for x in rs[key]:
        print ("  - x: ", x)
 - 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\", 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?



Paul ---



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.

Jimmy D.
Tier II API Support Engineer
Developer Portal

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

Visit Page