Full code
unknown
python
2 years ago
1.7 kB
6
Indexable
import json
import csv
def get_schema_field_names(filename):
"""Extract the list of fieldNames defined in the schema from a given JSON file."""
with open(filename, 'r') as file:
data = json.load(file)
schema = data.get("feed", {}).get("catalog", {}).get("schema", [])
return [field["fieldName"] for field in schema]
def get_item_keys(filename):
"""Extract the list of keys from the items in a given JSON file."""
with open(filename, 'r') as file:
data = json.load(file)
items = data.get("feed", {}).get("catalog", {}).get("add", {}).get("items", [])
if not items:
return []
return list(items[0].keys())
def check_mismatches_and_save_to_csv(filenames):
with open("error.csv", "w", newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["Filename", "Mismatch Type", "Field/Key Name"])
for filename in filenames:
schema_field_names = get_schema_field_names(filename)
item_keys = get_item_keys(filename)
# Check for schema fields not present in item keys
for field in schema_field_names:
if field not in item_keys:
writer.writerow([filename, "Schema Field Missing in Item", field])
# Check for item keys not present in schema fields
for key in item_keys:
if key not in schema_field_names:
writer.writerow([filename, "Item Key Missing in Schema", key])
if __name__ == "__main__":
filenames = ["file1.json", "file2.json", "file3.json", "file4.json"]
check_mismatches_and_save_to_csv(filenames)
print("Mismatch details have been saved to error.csv")
Editor is loading...