Why does my Discord bot show GIF links in ['link'] format instead of just the link?

I’m developing a Discord bot that sends GIF links when users type specific commands. I’ve set up a JSON file that includes various categories and their associated GIF URLs:

{
    "yup": [
        "https://tenor.com/view/mourinho-jose-mourinho-yes-yes-yes-yes-oh-yes-gif-745895004274181051"
    ],
    "calma": [
        "https://tenor.com/view/calma-relax-ronaldo-gif-16922523"
    ],
    "respect": [
        "https://tenor.com/view/respect-mou-mourinho-man-manchester-gif-19028709"
    ],
    "nospeak": [
        "https://tenor.com/view/josémourinho-big-trouble-if-i-speak-prefer-not-to-speak-interview-gif-16725048"
    ],
    "shush": [
        "https://tenor.com/view/jose-mourinho-ssh-manchester-united-mufc-shush-gif-10178656"
    ],
    "sui": [
        "https://tenor.com/view/sui-siu-ronaldo-football-portugal-gif-25997537"
    ]
}

In my main bot script, I load this JSON and set up a command that should send the corresponding GIF:

from discord.ext import commands
import discord
import json

links = json.load(open("gif.json"))

@bot.command(name="yup", aliases=["calma", "respect", "nospeak", "shush", "sui"])
async def send_gif(ctx):
    gif_link = links.get(ctx.invoked_with)
    await ctx.send(gif_link)

Unfortunately, the issue is that instead of just showing the URL, the bot returns it in a format wrapped in square brackets like [‘link’]. I even tried altering how I open the JSON file to include the ‘r’ parameter, but that didn’t resolve the issue. Can anyone shed light on why this is happening and how I can simply get the bot to send the URL without formatting?

ah i see the problem! your json uses arrays for each category, so links.get(ctx.invoked_with) returns the entire list. since you’ve only got one url per category, just grab the first element: gif_link = links.get(ctx.invoked_with)[0] and you’re good to go

Those square brackets show up because you’re sending a Python list instead of just the URL string. Discord sees the list and displays it with brackets - that’s how lists look when printed. Your JSON has each URL wrapped in an array, so when you use links.get(ctx.invoked_with), you’re grabbing the whole array. You need to pull the actual URL out of that array first. Try changing it to gif_link = links.get(ctx.invoked_with, [])[0] to get the first URL, or just restructure your JSON to store URLs as plain strings instead of wrapping them in arrays.

The square brackets appear because your JSON places each URL in an array. When Discord retrieves a Python list, it shows those brackets. By using links.get(ctx.invoked_with), you’re accessing the entire list instead of just the URL string. Since you only have one URL per command, you can either modify your code to extract the first item using links.get(ctx.invoked_with)[0], or change your JSON to hold URLs as plain strings without arrays. I recommend the first option for future flexibility with multiple GIFs.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.