Sunday, June 15, 2025

CompareJsonSchemas

 import json

from openpyxl import Workbook

from openpyxl.styles import Font



def load_json_schema(file_path):

    with open(file_path, 'r') as f:

        return json.load(f)



def flatten_schema_properties(schema, path=''):

    props = {}

    if 'properties' in schema:

        for key, value in schema['properties'].items():

            full_path = f"{path}.{key}" if path else key

            props[full_path] = {

                'type': value.get('type'),

                'enum': value.get('enum'),

                'format': value.get('format'),

                'maxLength': value.get('maxLength'),

                'minimum': value.get('minimum'),

                'maximum': value.get('maximum'),

            }

            if value.get('type') == 'object':

                nested = flatten_schema_properties(value, full_path)

                props.update(nested)

    return props



def compare_required_fields(schema1, schema2):

    req1 = set(schema1.get('required', []))

    req2 = set(schema2.get('required', []))


    added = req2 - req1

    removed = req1 - req2


    differences = []

    for field in added:

        differences.append((field, 'REQUIRED_ADDED', '', 'required'))

    for field in removed:

        differences.append((field, 'REQUIRED_REMOVED', 'required', ''))

    return differences



def compare_flattened_properties(flat1, flat2):

    all_keys = set(flat1.keys()) | set(flat2.keys())

    differences = []


    for key in all_keys:

        val1 = flat1.get(key)

        val2 = flat2.get(key)


        if key not in flat1:

            differences.append((key, 'ADDED', '', json.dumps(val2)))

        elif key not in flat2:

            differences.append((key, 'REMOVED', json.dumps(val1), ''))

        elif val1 != val2:

            differences.append((key, 'MODIFIED', json.dumps(val1), json.dumps(val2)))


    return differences



def write_differences_to_excel(differences, output_path):

    wb = Workbook()

    ws = wb.active

    ws.title = "Schema Differences"


    headers = ["Path", "Change Type", "Schema 1", "Schema 2"]

    ws.append(headers)


    for cell in ws[1]:

        cell.font = Font(bold=True)


    for diff in differences:

        ws.append(diff)


    wb.save(output_path)

    print(f"✅ Differences written to {output_path}")



if __name__ == "__main__":

    schema1_path = "schema1.json"

    schema2_path = "schema2.json"

    output_excel = "schema_differences.xlsx"


    schema1 = load_json_schema(schema1_path)

    schema2 = load_json_schema(schema2_path)


    flat1 = flatten_schema_properties(schema1)

    flat2 = flatten_schema_properties(schema2)


    prop_diffs = compare_flattened_properties(flat1, flat2)

    req_diffs = compare_required_fields(schema1, schema2)


    all_diffs = prop_diffs + req_diffs

    write_differences_to_excel(all_diffs, output_excel)


No comments:

Post a Comment