Trying to append variable to json returns an error in python

Question:

Disclaimer: The majority of this might not make sense, as basically all of this is just me trying to do something I have never done before.

I am trying to make a discord bot that has a "leaderboard" type system. But what I tried, gave me an error.

Heres my code.

import discord
from discord.ext import commands
import json
from json import loads
import os
TOKEN = 'Token'
bot = commands.Bot(command_prefix='!')


@bot.event
async def on_ready():
    print(f'Logged in as: {bot.user.name}')
    print(f'With ID: {bot.user.id}')


@bot.event
async def on_message(message):
    await bot.process_commands(message)
    if message.channel.id == 782989981752098886:
        if message.author == bot.user:
            return
        else:

            if ''.join(message.content.split()).lower() == "egg":
                os.chdir(r'Directory')
                with open('EggData.json', 'r') as file:
                    data = loads(file.read())
                try:
                    author = message.author.mention
                    usernameList = data
                    authorsAmount = usernameList[author]
                    print(authorsAmount)
                    usernameList[author] += 1
                    print(usernameList[author])
                    print(usernameList)
                    with open('EggData.json', 'w') as outfile:
                        json.dump(usernameList, outfile)
                    return
                except:
                    addUsersID = {f"{author}" : 0}
                    appendUserID = usernameList.append(addUsersID)
                    with open('EggData.json', 'w') as outfile:
                        json.dump(appendUserID, outfile)
                    author = message.author.mention
                    usernameList = data
                    authorsAmount = usernameList[author]
                    print(authorsAmount)
                    usernameList[author] += 1
                    print(usernameList[author])
                    print(usernameList)
                    with open('EggData.json', 'w') as outfile:
                        json.dump(usernameList, outfile)
                    return
                return
            else:
                await message.channel.send(
                    "{} You fool. You absolute buffoon, it is illegal to say anything other than 'egg' in this server. I hope you feel the shame in side you. Us only saying 'egg' in this channel brings peace to our server, and you thinking your above everyone? above ME? You {}, have messed up. I want you to take a long time to reflect your self.".format(message.author.mention, message.author.mention))
    else:
        return

bot.run(TOKEN)

It works just fine if I manually add the users ID and remove the try: and except:. But with the part that has the try: and except: is the part I tried to do in order to add the user to the list if they aren’t already on it.
Here is what the json looks like:
{"<@494664629570764810>": 11, "variblePlaceholder": 4, "numbernumbernumberPlaceholder": 26}
I will try to answer any questions you may have, as I am sure this isn’t very clear.

EDIT:

Forgot to say the error. The error I’m getting is
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

TRACEBACK ERROR:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:UsersnameAppDataLocalProgramsPythonPython39libsite-packagesdiscordclient.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "c:UsersnameDesktopPythonProofOfConceptJsonEggbot.py", line 27, in on_message
    data = loads(file.read())
  File "C:UsersnameAppDataLocalProgramsPythonPython39libjson__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:UsersnameAppDataLocalProgramsPythonPython39libjsondecoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:UsersnameAppDataLocalProgramsPythonPython39libjsondecoder.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)

Imgur link to the error: https://imgur.com/a/MQ0dtVc

Asked By: EchoTheAlpha

||

Answers:

Ok, slight error here. When using dictionaries (AKA json), you simply cannot use the .append() as dictionaries do not work that way. Dicttionaries (json) follow a key value pairing system, so you need to assign a key and value as show below…

Proper usage is:

my_dict["myvalue"]="another value"
#So in your case: 
usernameList[addUsersID]=#do something here...

The error you received is probably because there was nothing in the json/ the format has messed up. Also I noticed that you are making a discord bot. Consider joining the discord.py server, you can receive further support there. I found it useful a while back when I first started making bots. Hope the above helps!

Answered By: Viswamedha Nalabotu