[Solved] JSON Structural Difference


First and foremost, the example JSONs are not valid JSONs because keys in JSON has to be enclosed in ".

However, if you do have two valid JSON strings, you could parse them into a dictionary, then compare the structure of the dictionary using this function:

def equal_dict_structure(dict1, dict2):
    # If one of them, or neither are dictionaries, return False
    if type(dict1) is not dict or type(dict2) is not dict:
        return False

    dict1_keys = dict1.keys()
    dict2_keys = dict2.keys()

    if len(dict1_keys) != len(dict2_keys):
        # If they don't have the same amount of keys, they're not the same
        return False

    for key in dict1_keys:
        if key not in dict2_keys:
            return False

        dict1_value = dict1[key]
        dict2_value = dict2[key]

        if type(dict1_value) is dict:
            # If inner dictionary found, assert equality within inner dictionary
            if not equal_dict_structure(dict1_value, dict2_value):
                return False

    # No inequalities found
    return True

1

solved JSON Structural Difference