When importing a nested dictionary, stored in a JSON, how do you ensure keys/values are not duplicated, and values remain with their respective key?

  dictionary, discord.py, for-loop, json, python

With E3 coming around I’ve decided to whip up a quick command for my discord bot to keep track of our individual ratings for each conference. While I’ve essentially completed the command, I’m struggling with keeping each conference in their respective date key as well as struggling with values being duplicated alongside the duplicated keys.

I’ve tinkered with it for a few hours now, and this is the best I’ve gotten it so far:

    @commands.command()
async def e3scores(self, ctx):
    print('Called .e3scores')
    with open("e3scores.json", "r") as f:
        scoresDict = json.loads(f.read())

    embed = discord.Embed(title="**E3 Scores 2021**", description="A place to store your scores!", color=4047569)
    for day, pair in scoresDict.items():
        days = []
        scores = []

        days.append(day)
        for event, scoreGroup in pair.items():
            for gamer, score in scoreGroup.items():
                judgement = f'{gamer} - {score}'
                scores.append(judgement)
                finalScore = 'n'.join(scores)
            embed.add_field(name=day, value=event + finalScore, inline=True)

    await ctx.send(embed=embed)

Pulling from the following JSON dictionary:

 {
  "June 10th": {
    "Summer Games Fest": {
      "Roger": "B",
      "Samuel": "B",
      "Gabriel": "C",
      "Maria": "D+",
      "Janice": "C"
    },
    "Day of the Devs": {
      "Roger": "C+",
      "Samuel": "C-",
      "Gabriel": "B-",
      "Maria": "N/A",
      "Janice": "N/A"
    }
  },
  "June 11th": {
    "Netflix Geeked Week": {
      "Roger": "N/A",
      "Samuel": "N/A",
      "Gabriel": "N/A",
      "Maria": "N/A",
      "Janice": "N/A"
    },
    "Koch Primetime": {
      "Roger": "N/A",
      "Samuel": "N/A",
      "Gabriel": "N/A",
      "Maria": "N/A",
      "Janice": "N/A"
    },
    "IGN Expo": {
      "Roger": "N/A",
      "Samuel": "N/A",
      "Gabriel": "N/A",
      "Maria": "N/A",
      "Janice": "N/A"
    }
  }
}

Which outputs as:
https://i.imgur.com/KyjcduF.png

As you can see, the respective conferences are not being correctly paired up with their respective date key, and on top of that each successive conference is adding a new set of duplicate names and scores.

Any advice would be greatly appreciated, SO!

Source: Python Questions

LEAVE A COMMENT