如何用Python实现含空值/空字符串记录的错误JSON文件迁移(支持任意列数)
Hey there! Let's get your JSON record filtering logic working properly. Your current code is just printing column values—let's expand it to actually validate each record, split them into valid/error sets, and save them to the right files. Since your data has nested structures (like arrays and objects), we'll use Python's built-in json module instead of pandas—it's more flexible for handling arbitrary nested data.
Here's the complete solution:
First, we'll create a helper function to recursively check if any value in a record (including nested arrays/objects) is an empty string or null. Then we'll split the records and save them.
import json def has_invalid_value(value): """Check if a value (or any nested value) is empty string or null""" # Check basic case: empty string or null if value == "" or value is None: return True # If it's a list, check every item in the list elif isinstance(value, list): for item in value: if has_invalid_value(item): return True # If it's a dictionary, check every value in the dict elif isinstance(value, dict): for val in value.values(): if has_invalid_value(val): return True # No invalid values found return False # Read input JSON with open("Input_Files/find_error_record.json", "r") as input_file: all_records = json.load(input_file) # Split records into valid and error sets valid_records = [] error_records = [] for record in all_records: if has_invalid_value(record): error_records.append(record) else: valid_records.append(record) # Save valid records to output file with open("output.json", "w") as output_file: json.dump(valid_records, output_file, indent=4) # Save error records to error file with open("error.json", "w") as error_file: json.dump(error_records, error_file, indent=4)
How this works:
- Recursive Validation: The
has_invalid_valuefunction checks every part of a record, no matter how nested it is. For example, it'll catch empty strings inside theorg_namesarray orparsed_addressobject, just like in your input example. - Flexible for Any Columns: Since we're iterating through all keys/values dynamically, this code works with any number of columns or nested structures—no need to hardcode field names.
- Clean File Output: We use
json.dumpwithindent=4to keep the output JSON formatted nicely, matching your example files.
Testing with your sample input:
- The first record has empty strings in
first_name,last_name, andorg_names—it'll go toerror.json. - The second record has all valid values—it'll go to
output.json. - The third record has no empty/null values—it'll go to
output.json(matches your expected output).
If you ever need to exclude specific fields from the validation (e.g., allow empty strings in a certain column), you can modify the has_invalid_value function to skip those fields by name.
内容的提问来源于stack exchange,提问作者Naveen

