Saturday, May 31, 2025

ValidateAllJSONFilesinFolder

 import os

import json

import pandas as pd

from jsonschema import Draft7Validator


def load_json(filepath):

    with open(filepath, "r") as f:

        return json.load(f)


def validate_file(json_data, schema):

    validator = Draft7Validator(schema)

    errors = list(validator.iter_errors(json_data))

    results = []

    for error in errors:

        path = ".".join(str(p) for p in error.path) or "[root]"

        results.append((path, error.message))

    return results


def validate_all_jsons(folder_path, schema_path, report_path="validation_report.xlsx"):

    schema = load_json(schema_path)


    results = []

    for file in os.listdir(folder_path):

        if file.endswith(".json"):

            file_path = os.path.join(folder_path, file)

            try:

                json_data = load_json(file_path)

                errors = validate_file(json_data, schema)


                if errors:

                    for path, msg in errors:

                        results.append({

                            "File": file,

                            "Status": "Invalid",

                            "Path": path,

                            "Message": msg

                        })

                else:

                    results.append({

                        "File": file,

                        "Status": "Valid",

                        "Path": "",

                        "Message": ""

                    })

            except Exception as e:

                results.append({

                    "File": file,

                    "Status": "Error",

                    "Path": "",

                    "Message": str(e)

                })


    df = pd.DataFrame(results)

    df.to_excel(report_path, index=False)

    print(f"✅ Validation report saved to: {report_path}")


if __name__ == "__main__":

    # Update these paths

    JSON_FOLDER = "./data_samples"

    SCHEMA_FILE = "output_schema.json"

    OUTPUT_REPORT = "validation_report.xlsx"


    validate_all_jsons(JSON_FOLDER, SCHEMA_FILE, OUTPUT_REPORT)


No comments:

Post a Comment