My Json code:
{
"Axoland": {
"id": "axoland",
"president": 740107982842626078,
"coPresident": [],
"alliances": [],
"unconfirmedAlliances": []
},
"NukeLand": {
"id": "nukeland",
"president": 583297391093088269,
"coPresident": [],
"alliances": [],
"unconfirmedAlliances": []
},
"Greece": {
"id": "greece",
"president": 605355268854775828,
"coPresident": [],
"alliances": [],
"unconfirmedAlliances": []
},
"Noname": {
"id": "noname",
"president": 594794892585205790,
"coPresident": [],
"alliances": [],
"unconfirmedAlliances": []
},
"Gian Republic": {
"id": "gian republic",
"president": 749551090273615962,
"coPresident": [],
"alliances": [],
"unconfirmedAlliances": []
},
"Tinspit": {
"id": "tinspit",
"president": 697865613057458276,
"coPresident": [],
"alliances": [],
"unconfirmedAlliances": []
}
}
Python Code:
import json
def allc(ctx):
f = open('saved.json', "r")
data = json.loads(f.read())
await ctx.send(data) #This is just sending the message in discord. Same output in print().
Im making a discord bot (Note that you dont need experience in discord.py (i think) to help me in this. Just JSON and PYTHON knowledge.) for me and my friends but it uses json data. Now i have a command called allc which should print all of the names (Axoland, NukeLand, Greece, etc.). When i use the command all it returns is :
{'Axoland': {'id': 'axoland', 'president': 740107982842626078, 'coPresident': [], 'alliances': [], 'unconfirmedAlliances': []}, 'NukeLand': {'id': 'nukeland', 'president': 583297391093088269, 'coPresident': [], 'alliances': [], 'unconfirmedAlliances': []}, 'Greece': {'id': 'greece', 'president': 605355268854775828, 'coPresident': [], 'alliances': [], 'unconfirmedAlliances': []}, 'Noname': {'id': 'noname', 'president': 594794892585205790, 'coPresident': [], 'alliances': [], 'unconfirmedAlliances': []}, 'Gian Republic': {'id': 'gian republic', 'president': 749551090273615962, 'coPresident': [], 'alliances': [], 'unconfirmedAlliances': []}, 'Tinspit': {'id': 'tinspit', 'president': 697865613057458276, 'coPresident': [], 'alliances': [], 'unconfirmedAlliances': []}}
How do i get just the NAMES and not the data?
>Solution :
This JSON becomes a dictionary after you do the json.loads operation.
Every name is a key on this dictionary.
To get all names, then, using the data dict you have in the snippet, simply do data.keys()