Config.json number not counting as a number
Question:
I’m coding a program for Hypixel SkyBlock Minecraft that tells me the price of items that can be sold for a higher price.
The program is done but I’m running into a issue. I have made a config.json
file and it contains information like discord webhook, lowest price, and to notify the user if a item is found. I’m having problems with the lowest price function, basically when I set the lowest price to 1 in the config, the program doesn’t work and gets a traceback.
The traceback doesn’t matter because I know the issue. When I put the number into config it doesn’t work but when i set lowestprice = 1 in program manually it works like when I put the number into config the program thinks maybe the number is a text or something. Here’s the config code
with open("config.json", "r") as f:
data = json.load(f)
webhookread = data["webhook"]
notifyread = data["notify"]
lowestpriceread = data["lowestbin"]
WEBHOOK = webhookread
LOWEST_PRICE = lowestpriceread - THE ISSUE
NOTIFY = notifyread
Is there a way I could make the config file put the number as a real number not a text or any of that, so I can still use the config for numbers?
Answers:
Are the numbers in the json
file stored as strings or as numbers? if the json looks like this:
{
"lowestbin" : "123.45"
}
then the price is saved as string and will need to cast to a float type. This is simple to do:
lowestpriceread = float(data["lowestbin"])
Note: this code will thrown an exception if the data in the json cannot be converted into a float.
I’m coding a program for Hypixel SkyBlock Minecraft that tells me the price of items that can be sold for a higher price.
The program is done but I’m running into a issue. I have made a config.json
file and it contains information like discord webhook, lowest price, and to notify the user if a item is found. I’m having problems with the lowest price function, basically when I set the lowest price to 1 in the config, the program doesn’t work and gets a traceback.
The traceback doesn’t matter because I know the issue. When I put the number into config it doesn’t work but when i set lowestprice = 1 in program manually it works like when I put the number into config the program thinks maybe the number is a text or something. Here’s the config code
with open("config.json", "r") as f:
data = json.load(f)
webhookread = data["webhook"]
notifyread = data["notify"]
lowestpriceread = data["lowestbin"]
WEBHOOK = webhookread
LOWEST_PRICE = lowestpriceread - THE ISSUE
NOTIFY = notifyread
Is there a way I could make the config file put the number as a real number not a text or any of that, so I can still use the config for numbers?
Are the numbers in the json
file stored as strings or as numbers? if the json looks like this:
{
"lowestbin" : "123.45"
}
then the price is saved as string and will need to cast to a float type. This is simple to do:
lowestpriceread = float(data["lowestbin"])
Note: this code will thrown an exception if the data in the json cannot be converted into a float.