[Answered ]-How to iterate over json object in python containing dictionary and list

2👍

✅

This works for your example (python 3.5.2) but i don’t know if is the best approach (I mean, maybe there are some json parsing functions easier to use):

for v, k in itms.items():
    if not isinstance(k, list):
        for x, y in k.items():
            print(x,':', y)
    else:
        for i in k:
            for s, m in i.items():
                print(s,':', m)

with the result:

CandidateId : 9124657
BatchId : 456279
QPName : Domestic Data Entry Operator(SSC/Q2212)
CenterName : K
ProjectName : PKKVY
Candidate : Noori sahi 
TrainingProviderName : OrionEdutechPrivateLimited
Percentage : 0
PCName : obtain sufficient information from the customer /client to understand the need and perform initial task
MaxScore : 12
YourScore : 0
PCId : SRC/N3022_PC1
Percentage : 0
PCName : compares transcribed data, as displayed on a visual screen, document and corrects any errors with the source
MaxScore : 15
YourScore : 0
PCId : SRC/N3022_PC10
Percentage : 0
PCName : obtain help or advice from specialist if the problem is outside his/her area of competence or experience
MaxScore : 5
YourScore : 0
PCId : SSC/N3022_PC11

for python 2.7. only remove the parentheses from print

for v, k in itms.items():
    if not isinstance(k, list):
        for x, y in k.items():
            print x,':', y
    else:
        for i in k:
            for s, m in i.items():
                print s,':', m

0👍

To get data use:

import json
data = json.loads(request.body)
print data['PCTestScores']
for values in data['PCTestScores']:
    print values
    print values['PCId']
    print values['PCName']
    print values['Percentage']
    print values['MaxScore']
    print values['YourScore']

Leave a comment