3

So I have a Discord BOT using python and I am trying to get print the JSON information using the Enjin API. I can get it to print everything if I do print(Data), but as soon as I try displaying a certain bit of information, such as the variables_values, it doesn't display and gives me the following error:

Command raised an exception: TypeError: list indices must be integers or slices, not str

I have a previous function; called Status that uses a different API, and I can get it to work with that, but I just cannot get the following code to display the values in variables_values bracket. (As you can tell, I am not great with JSON | Also, ignore the xxxxx)

async def GetVariable(message):
    R = requests.get('http://www.xxxxxx.co.uk/api/m-shopping-purchases/m/47325895/user_id/19363576')
    Data = R.json()

    await client.say(Data["varaible_values"])

Here is what I want displayed, when I run the ?GetVariable command: jacobdallred - which is in the "variables_values":{"Steam64ID":"jacobdallred"} place.

Instead, it gives me the error previously mentioned.

Here is what it displays if I only client.say(Data): (Uploading to Hastebin to display nicer) https://hastebin.com/huwubiwawe.json

2
  • 1
    Your results R.json is an json array, not json object Commented Apr 24, 2018 at 16:43
  • 1
    Tip #1: Avoid using variable names like 'data', 'var', 'str' etc. Commented Apr 24, 2018 at 16:44

1 Answer 1

2

You need to access the first element of that json response array. Like this:

await client.say(Data[0]["items"][0]["variables_values"]["Steam64ID"])
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, thanks for giving an answer so quick, however I am getting the following error: Command raised an exception: KeyError: 'variables_values'

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.