-3

I am trying to print an specific key from a JSON file without success.

.json file is:

{
    "testing": [
        {
            "_name": "teste",
            "_profile_telphone": "212331233",
            "_age": "21"
        }
    ]
}

the function I'm using is:

def load(self):
    filename = self.profile_entry.get()
    if filename == "":
        msg = "Insert a profile name"
        messagebox.showinfo("Profile name is empty", msg)
        self.profile_entry.focus()
    else:
        with open('profile_'+filename+'.json', 'r') as outfile:
            data = json.load(outfile)
            print(data[filename])
            outfile.close()

with print(data[filename]) I can print entire

{
            "_name": "teste",
            "_profile_telphone": "212331233",
            "_age": "21"
        }

But how can I print only name or age for example?

barroso
  • 35
  • 1
  • 5
  • 1
    Does this answer your question? [How to extract a single value from JSON response?](https://stackoverflow.com/questions/12788217/how-to-extract-a-single-value-from-json-response) – buran Sep 22 '20 at 08:37
  • https://docs.python.org/3/tutorial/datastructures.html#dictionaries – jonrsharpe Sep 22 '20 at 08:37
  • @buran as you can see this are inside another key. First key is 'testing' get by [filename] print(data[filename]) inside it we have _name tried print(data[filename]['_name']) without sucess – barroso Sep 22 '20 at 08:44
  • Note that what you show as result of `print(data[filename])` is not correct. It will return a list with a dict inside it. You have dict inside list inside dict. – buran Sep 22 '20 at 08:50

1 Answers1

0

data is a list of JSONs - you have a json array there, in order to parse it you can do it with a for statement

for element in data[filename]:
    print(element[‘_name’])
    print(element[‘_profile_telphone’])
    print(element[‘_age’])
Alin Stelian
  • 861
  • 1
  • 6
  • 16