Reputation: 103
Not sure if this is even possible, but if so it would be awesome.
My code is:
url = "https://*myDomain*.zendesk.com/api/v2/organizations/*{id}*/tags.json"
req = urllib2.request(url)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, '[email protected]', 'password')
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)
urllib2.install_opener(opener)
response = urllib2.urlopen(req)
tagsA = response.read()
print tagsA
Now the data that is returned is:
{"tags":[]}
The API call itself returns the following
{
tags: []
}
However trying to access the list doesn't work as it seems to treat tagsA as a string. I would like it to treat it as a list so that I could check if 'tags' is empty.
Any help would be greatly appreciated!!!
Upvotes: 1
Views: 1246
Reputation: 8910
You need to json.load
(or json.loads
) the response body.
However, if you're going to do any kind of semi-complicated HTTP calls (authentication, cookies, etc.) in Python, you should really be using Kenneth Reitz's excellent Requests library (http://python-requests.org/) instead of urllib calls. Your entire code would become:
import requests
response = requests.get(url, auth=("my_username", "my_password"))
tagsA = response.json()
Upvotes: 0
Reputation: 474221
You need to load json string into python dictionary via json.loads():
import json
...
tagsA = json.loads(response.read())
print tagsA['tags']
Or, pass response
to json.load() (thanks to @J.F. Sebastian's comment):
tagsA = json.load(response)
print tagsA['tags']
Upvotes: 2