I've multiple json files with the following structure:
[
{
"vertrags_id": 1,
"versicherungsnummer": "770442924",
"vp_nummer": 581,
},
{
"vertrags_id":,
"versicherungsnummer": "7353924",
"vp_number": 63,
},
{
"vertrags_id": ,
"versicherungsnummer": "45262924",
"vp_number": 56,
}
]
I need to iterate through the files to know in which file I have whitespaces("") or no value as value for key "vertrags_id". Then i would like to append the filename to a list. I would be optimal to know in which line the whitespace or no value was found.
I've tried the following code:
lv = []
path = (r"C:...")
for file_name in [file for file in os.listdir(path) if file.endswith('.json')]:
with open(path file_name) as data_file:
data = json.load(data_file)
if data['vertrags_id'] == "" or None:
lv.append(file_name)
But I get following error message:
File "c:\Users\mallk\OneDrive\Desktop\Koenigswege\ProgrammingDell\Programming\postgres\test_find_value_json.py", line 16, in <module>
if data['vertrags_id'] == "" or None:
TypeError: list indices must be integers or slices, not str
Appreciate any hint.
CodePudding user response:
You can do this,
for file_name in [file for file in os.listdir(path) if file.endswith('.json')]:
with open(path file_name) as data_file:
data = json.load(data_file)
# this will check at least one value is empty or None
if not all([i['vertrags_id'] for i in data]):
lv.append(file_name)
CodePudding user response:
data
contains your whole json file. Given the structure you've specified, it is a list
, so you need to iterate over it.
for file_name in [file for file in os.listdir(path) if file.endswith('.json')]:
with open(path file_name) as data_file:
data = json.load(data_file)
# Close the file once we read the data
# Iterate over the records in data
for i, record in enumerate(data):
if (record['vertrags_id'] == "") or (record['vertrags_id'] is None):
lv.append("error in file {file_name}, at line {i}")
Notice I'm using enumerate
to also get the record number, that you can then use as you want.
CodePudding user response:
Judging by your data I think you're one loop short when processing the source:
for d in data:
if d['vertrags_id'] in ["", None]:
lv.append(file_name)
Also, the or
operator here doesn't work like you think it does. Instead use in
when comparing against multiple values.