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

I want to loop over this json object which I have got using web request.

import requests,ast
r = requests.get("some url")
data = r.text
data_dic = ast.literal_eval(data)

When I try to loop over the Json I am not able to fetch the expected output in key- Value pair. I want output like this

BatchId : 456279 
Candidate : Noori sahi
CandidateId :9124657 ...

and so on. Below code is My approach but dictionary inside the list is causing problem in looping.

for i in data_dic:
        for k,v in i.iteritems():
            print k,v

What I'm getting as error is 'str' object has no attribute 'iteritems'. What is the right approach for looping this kind of data.

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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']

3 Comments

I already tried it but output is same. I am having problem in only looping as it has one dictionary and anotherone is dictionary inside the list.
can you post your code? as dict_data is not in your JSON response. Or you talking about PCTestScores ?
import requests,json In [15]: r = requests.get('some url') In [16]: data = json.loads(r.content) In [17]: type(data) Out[17]: dict

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.