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
Hi @Emily Kern,
Please post more information. i.e request, response and the error.
Are you using jira - PyPI?
No, I am not familiar with that. I am using Python (2.7).
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).
And apologies, I was using request and json libraries. I added the request and error message in the above thread with @Josh Steckler
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()
It's a Python library, installed using pip.
pip install jira
I tried json.loads and I got a TypeError: Expected string or buffer.
It looks like you're new here. Sign in or register to get started.