def _clean_dict()

in bigquery_etl/dsar/dsar_response.py [0:0]


def _clean_dict(obj):
    """
    Recursively remove keys with value of `None` or empty dictionaries.

    We have a lot of empty columns in the table, this makes the output smaller.
    """
    if isinstance(obj, dict):
        cleaned_dict = {
            k: _clean_dict(v) for k, v in obj.items() if v is not None and v != "None"
        }
        # Remove keys where the value is an empty dictionary
        return {k: v for k, v in cleaned_dict.items() if v}
    elif isinstance(obj, list):
        cleaned_list = [_clean_dict(item) for item in obj]
        # Filter out dictionaries where both 'key' and 'value' are None or empty lists
        cleaned_list = [
            item
            for item in cleaned_list
            if not (
                isinstance(item, dict)
                and item.get("key") is None
                and item.get("value") is None
            )
        ]
    else:
        return str(obj)