Home > Software engineering >  Json data parsing using python to get the specific headers value
Json data parsing using python to get the specific headers value

Time:12-29

I have a Following Json File format in which the data will be having Dynamic update. I have to parse the Json file using python with the following scenario

If the status: "PASS", then the results of value should be maths,q1 ,q2

Kindly me with this scenario using python

{
    "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"
            },
            "status": "BLOCK"
        },
        "maths": {
            "q1": {
                "question": "5   7 = ?",
                "options": [
                    "10",
                    "11",
                    "12",
                    "13"
                ],
                "answer": "12"
            },
            "q2": {
                "question": "12 - 8 = ?",
                "options": [
                    "1",
                    "2",
                    "3",
                    "4"
                ],
                "answer": "4"
            },
            "status": "PASS"
        }
    }
}

CodePudding user response:

Hope below code gives you the expected output

import json

file = open('data.json')
data = json.load(file)
keys = []
for i in data['quiz']:
    if data['quiz'][i]['status'] != 'PASS':
        keys.append(i)
for key in keys:
    del data['quiz'][key]
print(data)
for key in data['quiz'].keys():
    print(key)
    for key1 in data['quiz'][key].keys():
        if key1 != 'status':
            print(key1)
  • Related