Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get name of JSON dictionary using automated python code

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{'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()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading