Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to print JSON data using Python

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

like image 360
James Smith Avatar asked Nov 29 '25 13:11

James Smith


1 Answers

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

await client.say(Data[0]["items"][0]["variables_values"]["Steam64ID"])
like image 163
drec4s Avatar answered Dec 02 '25 03:12

drec4s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!