data.json:
{
"bank": 0,
"injury": 0,
"inventory": [],
"money": 110,
"skills": {
"Charmer": {
"description": "Increase by using `/beg` - Increase's your chances in begging to charm the person",
"level": 1,
"per": 0
},
"Escapist": {
"description": "Increase by using `/flood` - Increase chance of escaping robbery attempts and floods!",
"level": 1,
"per": 0
},
"Fishing": {
"description": "Increase by using `/fish` and can increase earnings depending on you're level.",
"level": 1,
"per": 0
},
"Luck": {
"description": "Increaseable by drinking 'Potion of Luck' or using `/coinflip (0.25% chance)` and can change the odds of any minigame in your favor.",
"level": 1,
"per": 0
},
"Master Mind": {
"description": "Be a owner of the bot for more then 1 year - Cannot be increased by command's or editing of database.",
"level": 10,
"per": 100
}
}
}
I need to make a new skill and add it to the current set of skills
Not sure how to do it would appreciate any help!
I tried converting the dict to a list and using .append then converting it back to a dict but it didn’t work
DisguisedTG is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
7
To create new items in a dictionary, you can do dict["[item]"] = "value"
, and to load a JSON file as a dictionary, you can use the json
library. Therefore, you can do it as follows:
import json
with open("data.json") as data_file:
data = json.load(data_file)
data["skills"]["NewSkill"] = { # Replace with actual name
description = "NewSkill's description" # Replace with actual `description`
level = 10 # Replace with actual `level`
per = 0 # Replace with actual `per`
}
with open("data.json", "w+") as data_file:
json.dump(data, data_file)
4