Hi, I'm getting an error when I try to use the json library to properly read a REST API response. I checked the response and it was 200 (successful). Any idea why the json library can't "find" a json object to decode?
Hi Emily,
It might be helpful if you posted some of your code - at least the portion that received the HTTP response and decodes the JSON for translation into a python object.
Thanks!
-Josh
Here we go (sorry about that):
BASE = 'link2theAPI/rest/api/content?'
somePage = {'title': 'pageIWant'}
getThatPage = requests.get(BASE,params=somePage)
pageId = getThatPage.json()['results'][0]
I am using Python's json and requests libraries. The pageId variable is a stand-in for retrieving information on the page, including its id (it's also to get familiar with the format of the json object).
Error message is: ValueError: No JSON object could be decoded
And I made sure I was getting a Response (200).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Emily -
You may need to alter your code to be like this (untested)
getThatPage = json.loads(requests.get(BASE,params=somePage))
pageId = getThatPage['results'][0]
Here is a full snippet of code that does work for me. It's from a Jira script I wrote, but the theory is similar. I only used the http2lib, base64 and json libraries here.
getheaders = {
"Authorization": "Basic " + base64.encodestring(jirausername + ":" + jirapassword),
"Accept": "application/json"
}
jiraconnection = httplib2.Http()
try:
(getcomponents_resp, getcomponents_content) = jiraconnection.request(jirabaseurl + "/rest/api/2/project/" + jiraprojectkey + "/components", "GET",headers=getheaders)
except:
print("ERROR: Could not retreive list of components from Jira. HTTP getcomponents_response below\n")
print(getcomponents_resp)
print(getcomponents_content)
exit()
componentsList = []
if getcomponents_resp["status"] == "200":
getcomponents_contentJson = json.loads(getcomponents_content)
for componentObj in getcomponents_contentJson:
componentsList.append(componentObj["name"])
else:
print ("ERROR: HTTP getcomponents_response Code invalid. HTTP getcomponents_response below\n")
print(getcomponents_resp)
print(getcomponents_content)
exit()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tried json.loads and I got a TypeError: Expected string or buffer.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the help. I just separated the .json() from the query ([results][0]) and it worked. I'm not sure why doing so made the difference, but I do get a json object now.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Emily Kern,
Please post more information. i.e request, response and the error.
Are you using jira - PyPI?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
And apologies, I was using request and json libraries. I added the request and error message in the above thread with @Josh Steckler
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's a Python library, installed using pip.
pip install jira
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.