Python json can't read REST API response (200)
Asked by Emily Kern:

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?


Answers:
author: josh
body: 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

comments: author: Emily Kern
body: 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).


author: josh
body: 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()
```


author: Emily Kern
body: I tried json.loads and I got a TypeError: Expected string or buffer.


author: Emily Kern
body: 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.


author: Edwin Kyalangalilwa
body: Hi [@Emily Kern](/t5/user/viewprofilepage/user-id/2505844),

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

Are you using [jira - PyPI](https://pypi.org/project/jira/)?

comments: author: Emily Kern
body: No, I am not familiar with that. I am using Python (2.7).


author: Emily Kern
body: And apologies, I was using request and json libraries. I added the request and error message in the above thread with [@josh](/t5/user/viewprofilepage/user-id/581772)


author: Edwin Kyalangalilwa
body: It's a Python library, installed using pip.

```
pip install jira
```


author: Emily Kern
body: I'll have a look at that. Thank you! That sounds very helpful.