Home > front end >  How can I debug a Python JSONdecode error?
How can I debug a Python JSONdecode error?

Time:06-23

Sorry for the weird question I couldn't submit it other way. I'm trying to make a chatbot command. But I keep getting stupid json errors.

Here's my code:

@client.command()
async def chatbot(ctx, *, msg):
    chatbot1 = requests.get(f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}")
    resp = chatbot1.json()
    await ctx.send(resp)

Here's the error:

Ignoring exception in command chatbott:
Traceback (most recent call last):
  File "/home/runner/m/venv/lib/python3.8/site-packages/requests/models.py", line 972, in json
    return complexjson.loads(self.text, **kwargs)
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/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)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 50, in chatbott
    resp = chatbot1.json()
  File "/home/runner/m/venv/lib/python3.8/site-packages/requests/models.py", line 976, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/home/runner/m/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Fixed it.

Fix:

@client.command()
async def chatbot(ctx, msg):
  url = f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}"
  data = requests.get(url)
  await ctx.send(data.text)

CodePudding user response:

While it may not always be easy to identify json errors, the error you're getting is on the very first character. Expecting value: line 1 column 1 (char 0) My guess is that you're not receiving a jsone message at all. To begin with, check the resp.status_code to see if you are getting a 200 response or something else. You can also debug by printing/logging the response's text using resp.text, which may give you additional leads.

CodePudding user response:

Found the solution.

@client.command()
async def chatbot(ctx, msg):
  url = f"https://chatbot-api.therealenny1.repl.co/?message={quote(msg)}"
  data = requests.get(url)
  await ctx.send(data.text)
  • Related