Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Python json can't read REST API response (200)

Emily Kern January 24, 2019

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?

2 answers

1 vote
Josh Steckler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2019

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

Emily Kern January 24, 2019

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).

Josh Steckler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2019

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()
Emily Kern January 25, 2019

I tried json.loads and I got a TypeError: Expected string or buffer.

Emily Kern January 25, 2019

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.

0 votes
Edwin Kyalangalilwa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2019

Hi @Emily Kern,

Please post more information. i.e request, response and the error.

Are you using jira - PyPI?

Emily Kern January 24, 2019

No, I am not familiar with that. I am using Python (2.7).

Emily Kern January 24, 2019

And apologies, I was using request and json libraries. I added the request and error message in the above thread with @josh

Edwin Kyalangalilwa
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 24, 2019

It's a Python library, installed using pip.

pip install jira
Emily Kern January 25, 2019

I'll have a look at that. Thank you! That sounds very helpful.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events