0

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)
8
  • 2
    "Expecting value: line 1 column 1 (char 0)" So it seems like the error occurs on the very first character. Often, that happens if the response is either empty or not JSON at all, e.g., some HTML or XML or something Commented Jun 21, 2022 at 20:56
  • Print the text of the response. Commented Jun 21, 2022 at 20:57
  • Idk if it's a json you guys can check it by changing the text Commented Jun 21, 2022 at 21:01
  • chatbot-api.therealenny1.repl.co/?message=hi Commented Jun 21, 2022 at 21:02
  • just test thattt Commented Jun 21, 2022 at 21:02

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting a 200 response code
If you do not have enough details to answer the question, you shouldn't answer it. If it is the case that the json message is empty, then this will be a duplicate.
Please man can you just leave me alone just one time. Everytime I make a post you always come here like a karen talking to me what I should do and what I should not. I'm simply asking a question to something I don't know.
1

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)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

To answer the title question of "How can I debug a Python JSONdecode error?" (having arrived here asking that question), I have found that there are many online JSON validators, eg. https://jsonlint.com/.

Copy-paste in your erroneous JSON string, and the location of any errors will be highlighted.

Comments

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.