简体   繁体   中英

Validate nested json keys with list of keys

I have list of valid keys I have a nested json which I normalised into a df

After normalisation I end up with column names like "ItemProductContentWeightUoMGr"

In the list with valid keys I have an element called "UoM"

As you see I can't compare the two, which is what I am trying here:

try:
    for key in self.df.columns:
        if key not in self.valid_keys:
            raise KeyError(f'Key {key} is not present in list of valid keys')
except Exception as e:
    logging.error(f'Something went wrong validating the keys.\nError: {e}') 

suppose df.columns can not be changed.. How could I do this validation of any nested json message against a simple list of keys?

Would be helpful to see what is actually logged within e for debugging in case of errors. Anyways as I read your usecase there is no error or result as you would have to flip both "lists" of df.columns and valid_keys because you are looking to find a smaller pattern in a bigger string:

 for valid_key in valid_keys:
     if valid_key not in list(df.columns):
         ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM