Home > database >  How to Select the Minimum Attribute in a JSON?
How to Select the Minimum Attribute in a JSON?

Time:10-06

I'm developing a discord bot for fetching the floor price on Solanart. I'm using this JSON: https://qzlsklfacc.medianetwork.cloud/nft_for_sale?collection=cryptocavemen

How do i select the minimum price in this JSON file ?

The program needs to find the minimum price everytime when the user enters !floor in discord.

Here is my code. It's all working well but i can't manage how to get the minimum of price attribute.

import discord
import os
import json
import urllib

my_secret = os.environ['TOKEN']
client = discord.Client()

with urllib.request.urlopen("https://qzlsklfacc.medianetwork.cloud/nft_for_sale? 
collection=cryptocavemen") as url:
data = json.loads(url.read().decode())


@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('!floor'):

    ??????????

    await message.channel.send("The Floor Price is: "   #floor price data )

client.run(my_secret)

Need your help ! Thanks !

CodePudding user response:

Don't think about it as a JSON file, once it is ready as json.loads it is just a list of dictionaries where you can check the price attribute for each of them.

If you just want to return the lowest price, you can combine the map and min functions to obtain the result in a single line:

floor_price = min(map(lambda x: x['price'], data))
  • Related