简体   繁体   中英

“Expecting property name enclosed in double quotes” in Discord

I am coding a discord bot to use with my friends and I am getting a weird message on one of my cogs. The code works perfect on one cog and doesn't at all on the other.

I've tried rewriting the command and trying to get the data in a different way to no avail.

import discord
import json
import os
from discord.ext import commands

class Inventory(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command(aliases=['iindex', 'ii'])
    async def itemindex(self, ctx, arg1='nd', arg2='nd'):
        await ctx.channel.purge(limit=1)
        if arg1 == 'nd':
            embed=discord.Embed(title="insert title", description='insert text', color=0x02a5fd)
            embed.set_footer(text="insert footer")
            await ctx.send(embed=embed)

        if arg1 == 'standard':

            if arg2 == 'nd':
                _n = 1
                _a = 0
                embed=discord.Embed(title="insert title", color=0x02a5fd)
                with open('database.json', 'r') as f:
                    data = json.load(f) #error on this line

                while _a < 20:
                    for item in data['items']['standard']:
                        if data['items']['standard'][item]['itemid'] == _n:
                            name = data['items']['standard'][item]['name']
                            embed.add_field(name=f'{name}', value=f'ID: {_n}', inline=False)
                            _n += 1
                            _a += 1

                embed.set_footer(text="insert footer")
                await ctx.send(embed=embed)

def setup(client):
    client.add_cog(Inventory(client))

What it should be doing is loading the file and looping through the items in the key, but it won't even get to that point. Instead, it gives me this error:

Ignoring exception in command itemindex:
Traceback (most recent call last):
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\stars\Documents\Projects\Discord\Neximator\cogs\inventory.py", line 30, in itemindex
    data = json.load(f)
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 355, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 43 column 5 (char 1218)

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

Traceback (most recent call last):
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 859, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 725, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\stars\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting property name enclosed in double quotes: line 43 column 5 (char 1218)

Am I doing something wrong or is this a bug?

Edit: I realize now that this could be a json error because I am pretty new to JSON and don't really know all of the rules. Here is the database.json file I'm using:

{
  "nicknames": {
    "discordname": "nickname",
  },
  "items": {
    "standard": {
      "slimeball": {
        "name": "Slimeball",
        "description": "Ball of sticky, viscous slime dropped by... well... slimes.",
        "rarity": "D",
        "itemid": 1
      }
    },
    "weapon": {
      "wooden_dagger": {
        "name": "Wooden Dagger",
        "description": "A stick with an edge lazily carved into it.",
        "type": "sword",
        "damage": "1d3",
        "accuracy": "1d20",
        "weaponid": 1
      },
      "wooden_gaunlets": {
        "name": "Wooden Gaunlets",
        "description": "Two logs with holes for your hands.  There are several designs lazily cut into it.",
        "type": "melee",
        "damage": "2d2",
        "accuracy": "2d10",
        "weaponid": 2
      },
      "wooden_bow": {
        "name": "Wooden Bow",
        "description": "A stick with a thin strand of elastic threaded through the ends creating an arch.",
        "type": "ranged",
        "damage": "1d4",
        "accuracy": "1d25",
        "weaponid": 3
      },
    }
  },
  "enemies": {
  }
}

This is happening because f is not valid JSON.

It gives you a pretty clear error as to why. Expecting property name enclosed in double quotes

Without seeing your json file I can't really say more. But I am assuming database.json is not valid JSON. My guess is that you are using single quotes on your keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM