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

How to traverse a json file in python and find the key name using value

can anyone please help me to find the solution
I am having a JSON file like below

    {
   "Parent1": ["Child1", "Child2","Child5"],
   "Parent2": ["Child3", "Child4","Child5"]
    }

expectation: Python code to find the parent name using child name

User input: Child1
Expected output : Parent1

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

OR

User input: Child5
Expected output : Parent1,Parent2

>Solution :

This is an apporach you can use

choice = input("Enter search string ")
ans = []
for i,j in data.items():
    if choice in j:
        ans.append(i)

ans will now contain the keys you need as a list

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