Stack Exchange
Unicode Decode Error for JSON in python
I am trying to send a GET request to a URL that I know returns data in the form of JSON using python. However, I keep getting a Unicode Decode Error on line 21:
Traceback (most recent call last):
  File "MeetupQueryScript.py", line 21, in <module>
    member = json.load(object)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 278, in load
    **kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 366, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 382, in raw_decode
    obj, end = self.scan_once(s, idx)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe7 in position 20: invalid continuation byte

Any idea whats up?
import urllib2
import json

key = '#########'

url = "https://api.meetup.com/2/groups?&sign=true&zip=78705&page=1&key=" + key + "&&sign=true"
object  = urllib2.urlopen(url)
#print [method for method in dir(object) if callable(getattr(object, method))]                                                  
#print object.next()                                                                                                            
groupList = json.load(object)["results"]
groupIDs = []

for group in groupList:
    groupIDs.append(group["id"])

members = []

for id in groupIDs:
    url = "https://api.meetup.com/2/members?&sign=true&group_id=" + str(id) + "&key=" + key
    object = urllib2.urlopen(url)
    member = json.load(object)
    members.append(member)

print members


Off topic: avoid using object as variable name.

The data you're getting from meetup is latin1 encoded (print '\xe7'.decode('latin1') results in 'ç'). I tried your code with member = json.load(object,encoding='latin1') and it worked.
I'd recommend requests though, due to it having a more convenient interface:
import requests

url = "https://api.meetup.com/2/groups?&sign=true&zip=78705&page=1&key=" + key + "&&sign=true"
groupList = requests.get(url).json()["results"]
groupIDs = [] 

for group in groupList:
    groupIDs.append(group["id"])

    members = []

for id in groupIDs:
    url = "https://api.meetup.com/2/members?&sign=true&group_id=" + str(id) + "&key=" + key
    member = requests.get(url).json()
    members.append(member)

print members

And I second the recommendation not to hide object in your code. Also check out the python style guidelines.
