Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i need a help in parsing json files using python.this is my sample json file

i have tried the following code,but itz throwing a key error if any attribute value is null, how do i skip those attributes and continue parsing for remaining data.

import csv

with open("global.json") as file:
data = json.load(file)

with open("single.csv", "w") as file:
csv_file = csv.writer(file)
csv_file.writerow 

for item in data:
    csv_file.writerow([item['policyNumber'],
                       item['policyType'],
                       item['policyStatus'],
                       item['termEffectiveDate'],
                       item['sourceSystem'],
                       item['agency'],
                       item['policyAddress'][0]['address1'],
                       item['policyAddress'][0]['zipCode'],
                       item['policyAddress'][0]['city'],
                       item['policyAddress'][0]['state'],
                       item['consumers'][0]['matchFlag'],
                       item['consumers'][0]['firstName'],
                       item['consumers'][0]['lastName'],
                       item['consumers'][0]['eid'],
share|improve this question
add comment

1 Answer

Use the dict.get() method to get a default value if a key is not defined:

item.get('policyNumber', ''),

and perhaps:

item.get('policyAddress', [{}])[0].get('zipCode', ''),
# ...
item.get('consumers', [{}])[0].get('matchFlag', ''),

to default to an empty dictionary for the nested list items. The latter can still fail if there is a policyAddress or consumers key but the value is an empty list.

You can preprocess each item a little:

for item in data:
    if not item.get('policyAddress'):
        item['policyAddress'] = [{'address1': '', 'zipCode': '', 'city': '', 'state': ''})
    if not item.get('consumers'):
        item['consumers'] = [{'matchFlag': '', 'firstName': '', 'lastName': '', 'eid': ''})

then use:

item['policyAddress'][0].get('address1', '')

etc.

share|improve this answer
    
thanks Martijn will try out this ...so for eg:item['policyAddress'][0]['zipCode'], should be replaced by item.get('policyAddress','','zipCode','') –  user3514648 2 days ago
    
@user3514648: no; you'd use item.get('policyAddress', [{}])[0].get('zipCode', ''); this returns [{}] (a list with one empty dictionary) as the default for policyAddress, then returns an empty string if the result has no zipCode key. –  Martijn Pieters 2 days ago
    
The answer provided by Martijn Pieters is a great answer. If I understand what's you're trying to do, you want to convert a JSON into a csv, and this question has been answered here. –  aRkadeFR yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.