[Python] Is there an easy way to check if an object is JSON serializable in python?

Discussão em 'Python' iniciado por Stack, Junho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I was trying to check if objects were JSON serializable or not because I had a dictionary that had a bunch of things and at this point its just easier to loop through its keys and find if they are JSON serializable and remove them. Something like (though this checks if its function):

    def remove_functions_from_dict(arg_dict):
    '''
    Removes functions from dictionary and returns modified dictionary
    '''
    keys_to_delete = []
    for key,value in arg_dict.items():
    if hasattr(value, '__call__'):
    keys_to_delete.append(key)
    for key in keys_to_delete:
    del arg_dict[key]
    return arg_dict


    is there a way that the if statement instead check for JSON serializable objects and deletes them from the dictionary in a similar way to above?

    Continue reading...

Compartilhe esta Página