So I am reading through two json files to check if the keys file name and file size exist. In one of the files i only have the key filename and not file size. When running my script it keeps through a KeyError , i would like to instead make it print out the file name/names that do not have the key file size.
The error i get is :
if data_current['File Size'] not in data_current:
KeyError: 'File Size'
file1.json
{"File Name": "personDetails.json Exists", "File Size": "7484"}
{"File Name": "agent.json Not Exists"}
file2.json
{"File Name": "personDetails.json Exists", "File Size": "7484"}
{"File Name": "agent.json Not Exists", "File Size": "9484"}
My code is as follows :
with open('file1.json', 'r') as f, open('file2.json', 'r') as g:
for cd, pd in zip(f, g):
data_current = json.loads(cd)
data_previous = json.loads(pd)
if data_current['File Size'] not in data_current:
data_current['File Size'] = 0
if data_current['File Name'] != data_previous['File Name']: # If file names do not match
print " File names do not match"
elif data_current['File Name'] == data_previous['File Name']: # If file names match
print " File names match"
elif data_current['File Size'] == data_previous['File Size']: # If file sizes match
print "File sizes match"
elif data_current['File Size'] != data_previous['File Size']: #
print "File size is missing"
else:
print ("Everything is fine")