-1

I know these kinds of questions have been answered,but the solutions provided doesn't work for me at all. I want to retrieve all the keys in an JSON file, at every level, not just at the first level.

Let say I want to get all the keys from the following JSON file

{
    "quiz": {
        "sport": {
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            }
        }
    }
}

My progress so far

for k,v in json_object.items():
    print(v.keys())

1 Answers1

1

Store you json content as dict in a variable i.e. data as:

data = {
    "quiz": {
        "sport": {
            "q1": {
                "question": "Which one is correct team name in NBA?",
                "options": [
                    "New York Bulls",
                    "Los Angeles Kings",
                    "Golden State Warriros",
                    "Huston Rocket"
                ],
                "answer": "Huston Rocket"
            }
        },
        "maths": {
            "q1": {
                "question": "5 + 7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            }
        }
    }
}

Use the below recursive method to get all the keys...

def get_keys(d, indent=0, parent=""):
    print("{}{}{}{}".format(" "*indent*4, parent, " => " if parent else "", list(d.keys())))

    for key in d:
        if isinstance(d[key], dict):
            get_keys(d[key], indent=indent+1, parent=key)

When you call the method as below:

get_keys(data)                                                                                                                                                                                        

you would get Output as:

['quiz']
    quiz => ['sport', 'maths']
        sport => ['q1']
            q1 => ['question', 'options', 'answer']
        maths => ['q1', 'q2']
            q1 => ['question', 'options', 'answer']
            q2 => ['question', 'options', 'answer']
Gahan
  • 4,075
  • 4
  • 24
  • 44