0

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'],
sshashank124
  • 31,495
  • 9
  • 67
  • 76
user3514648
  • 101
  • 3
  • 15

1 Answers1

0

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • thanks Martijn will try out this ...so for eg:item['policyAddress'][0]['zipCode'], should be replaced by item.get('policyAddress','','zipCode','') – user3514648 May 14 '14 at 09:34
  • @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 May 14 '14 at 09:36
  • 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](http://stackoverflow.com/questions/1871524/convert-from-json-to-csv-using-python). – FlogFR May 15 '14 at 11:20