Search code examples
pythonjsonfind

Python - find whitespace or none value in multiple json files


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.


Solution

  • 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.