0

I got a program from a formerly colleague and now should maintain it. This python script asks our Jira instance with a given jql ( on the API ). The return is a list of all issues, which are matching the search criteria. But now it's not working, and I receive on the server ( Ubuntu ) and on my local windows PC a Json error message. note : it ran for about a year not, but back then it worked.

Here is what the script looks like :

import json
import subprocess

jiraSerachUrl = "https://ourJiraInstance.net/rest/api/2/search?jql=key%20=%20%22TEST-123%22"
jiraResponse = subprocess.Popen(["curl","-l","-s","-u", "jiraUser"+":"+"jiraUserPassword", "-X", "GET", jiraSerachUrl ],stdout=subprocess.PIPE,shell=True).communicate()[0]
## shell=True only added for Windows Instance
print(type(jiraResponse))
##print =  <class 'bytes'>
print(jiraResponse)
## print = b''
jiraJsonResponse = json.loads(jiraResponse.decode('utf-8'))
print(jiraJsonResponse)

The jql/jira search address returns the following (shorted answer, all fields of the task are returned):

{"expand":"names,schema","startAt":0,"maxResults":50,"total":1,"issues": [{"expand":"operations,versionedRepresentations,editmeta,changelog,transitions,renderedFields", "id":"145936","self":"https://ourJiraInstance.net/rest/api/2/issue/145936","key":"TEST-123","fields":{"parent": ...

The Error on the Windows PC is the following

Traceback (most recent call last): File "C:\Users\User\Desktop\test.py", line 10, in jiraJsonResponse = json.loads(jiraResponse.decode('utf-8')) File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\json__init__.py", line 319, in loads return _default_decoder.decode(s) File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

This is the error on the Ubuntu Server ( running the same script )

Traceback (most recent call last): File "searchJira.py", line 33, in jiraJsonResponse = json.loads(jiraResponse) File "/usr/lib/python2.7/json/init.py", line 338, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 366, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

So far I tried to change the Json load to simpleJson, but with the same result. Changing the format to which Json should decode ( e.g. unicode ) took no effect.

8
  • Is there any output from print(jiraResponse) or it is empty or None object? The error message indicates that the object jiraResponse may be None. Commented Dec 2, 2016 at 6:06
  • the output is in the comment below -> b'' Commented Dec 2, 2016 at 6:08
  • Then I think the root cause is that the object to be decoded is empty or None. It leads to a ValueError. If there is truly no data returned from jql. Then make a logical judgement to avoid the error. if jiraJsonResponse: jiraJsonResponse = json.loads(jiraResponse.decode('utf-8')) print(jiraJsonResponse) Commented Dec 2, 2016 at 6:14
  • Looks like the bytestring is causing the error. try to encode the byte string before you use. In this case you can do jiraJsonResponse.encode('utf-8') and then use it for other json operations. Commented Dec 2, 2016 at 6:22
  • @Theresa - the return should not be empty. when I access the webpage there is an issue class serialized. Commented Dec 2, 2016 at 6:31

1 Answer 1

0

I have tried a bit and finaly got it. by replacing curl with responses i got finally the result I wanted. my request looks now like this :

r = requests.get(jiraSerachUrl,auth=HTTPBasicAuth(user, password), verify=False) 
jiraJsonResponse=json.loads(r.text)
Sign up to request clarification or add additional context in comments.

4 Comments

FYI, you can do r.json() directly
with r.json the return is from <class 'method'> when I do json.loads(r.text) the return is <class 'dict'> and I need the dict for an iteration
Notice the () in r.json()
ad okay, that was my fault. Thanks ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.