-1

I'm not a software dev; I am self taught and learn through trial and error so this may be clear to some of you whereas I just don't know what I'm doing wrong.

That being said, I have a project I was working on a few months ago... which is now erroring out during runtime. I look at the traceback, and it appears to be complaining about the json decoder:

Traceback (most recent call last):
  File "r3ceive.py", line 222, in <module>
    beginRec()
  File "r3ceive.py", line 59, in beginRec
    iterateList()
  File "r3ceive.py", line 71, in iterateList
    pullConv(id)
  File "r3ceive.py", line 77, in pullConv
    response=requests.get(url + id, headers=h, params=p).json()
  File "C:\Users\mnowicky\.virtualenvs\k3nect_env-VU1MvjZD\lib\site-packages\requests\models.py", line 890, in json
    self.content.decode(encoding), **kwargs
  File "c:\program files\python37\Lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "c:\program files\python37\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\program files\python37\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Here is the line it's complaining about (line 77, the request interestingly):

response=requests.get(url + id, headers=h, params=p).json()

And here is the entire function:

#For each id in reclist.txt, pull their conversation and latest reply
def pullConv(id):
    global cached, latest_key, refDict, direction
    response=requests.get(url + id, headers=h, params=p).json()
    inbound_dict = {}
    for item in response['messages']:
        conv_id = item['conversationId']
        attach = item['attachments']
        if conv_id not in inbound_dict:
            inbound_dict[conv_id]={item['id'] : {'msg_id' : item['id'], 'body' : item['body']}}
        else:
            inbound_dict[conv_id][item['id']] = {'msg_id' : item['id'], 'body' : item['body']}
    cached=inbound_dict
    cached={k: get_latest(cached[k]) for k in cached.keys()}
    latest_key=max(cached.keys())
    reply=str(cached[latest_key])
    z=cached[latest_key]
    z=str(z)
    
    # check for attachment
    if attach:
            attachments(attach, conv_id)
    if not attach:
        pass
    
    # check direction, that the message is indeed incoming
    checkOutgoing(z, id)
    
    if direction=='in':
        m=str(refDict[id])
        if m!=z:
            Logger.recLog('New reponse detected for id: ' + id, 0)
            u={id: z}
            refDict.update(u)
            with open('shelves/recDic.json', 'w') as outfile:
                json.dump(refDict, outfile)
                needlesNotify(z, id)
        else:
            pass

So there's got to be something wrong with the json dependency, but I can't update it or anything...

I'm using python 3.7 with pipenv. I have some experience with Node.js so the whole pipenv package management makes sense to me. However I never knew anything about best practice regarding package version management... so this is what my pipfile looks like (lol):

[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
mypy = "*"

[packages]
pyodbc = "*"
configparser = "*"
datetime = "*"
requests = "*"
tqdm = "*"
colorama = "*"
progress = "*"
json-decode = "*"
pprint = "*"
json = "*"
pil = "*"

[requires]
python_version = "3.7"

My packages and dependencies are so mis-managed that I can never get a lock on the pipfile to update anything. It appears that it can't locate any version for the json package, and that's why it's erroring, because of the .json() at the end of the request?

When I try to do a pipenv update, or pipenv lock --clear, or anything at all really, I get this:

[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  First try clearing your dependency cache with $ pipenv lock --clear, then try the original command again.
 Alternatively, you can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: ERROR: Could not find a version that matches json
No versions found

So I guess I'm looking for some guidance and advice... as in, what should I do to try to resolve this. What is the best practice for managing the dependencies and such. What am I doing wrong here?

Thank you for taking a look!

boog
  • 472
  • 1
  • 5
  • 23
  • The purpose of this code is an sms texting application. It has a sender module and a receiving module. This is the receiving module. It iterates through a list of unique keys and checks for new messages. Interestingly, it doesnt error out on the first loop through the list for this particular conversation ID, but it does on the second. – boog Jun 26 '20 at 18:19
  • 1
    It looks like calling `.json()` attemps to parse an empty string. You should check whether the response to your request actually succeeded (at least if it returned HTTP 200). – michalwa Jun 26 '20 at 18:21
  • The response does succeed, it has to because the code actually works to receive an sms message from the api- it's on subsequent iterations through the list of "conversation IDs" that it will fail. It seems to be random, in that it will successfully check a particular conversation for new messages 5, 6 7 times... but then eventually it fails with that error – boog Jun 26 '20 at 18:26
  • Maybe try setting up a logger (unless you already have) and taking a look at what is passed to the JSON parser? – michalwa Jun 26 '20 at 18:27
  • Or I mean it's pretty clear that the response body is just empty, so maybe just check for that and don't call `json()` if it's an empty string. – michalwa Jun 26 '20 at 18:30
  • I do have a logger- that's a good idea because strangely enough I didn't include the actual response in the log lol... trying that now. Will check the response for each time it fails – boog Jun 26 '20 at 18:30
  • @michalwa so something like, if json.response ='' pass? – boog Jun 26 '20 at 18:31
  • `response = requests.get(...)` then `if response.content == '': return` or just do something other than `.json()` – michalwa Jun 26 '20 at 18:33
  • You should know that `pass` doesn't do anything and you use `return` to exit out of a function. – michalwa Jun 26 '20 at 18:38
  • cool... yeah I added the logging and for some reason, as expected, the json response is empty occasionally. Doesn't seem to be any pattern or reason as to why. So I'll have to add that check in then. Thank you for the tip- though I would like to know how I'm supposed to manage my packages so they can be updated! lol – boog Jun 26 '20 at 18:39
  • You have a few built-in Python packages in your `packages` section: `json` (pretty sure you don't need `json-decode` either) and `datetime`. Maybe try removing those? I don't have a lot of experience with pipenv, so I don't want to pollute the comments with bad guesses. – michalwa Jun 26 '20 at 18:44
  • thanks for your help! – boog Jun 26 '20 at 18:50

1 Answers1

0

The response to my request was occasionally showing up blank, causing the .json() to fail.

Had to add checking to make sure the response contained something.

boog
  • 472
  • 1
  • 5
  • 23