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

@shx2’s answer is good enough, but it’s better to specify which exceptions you’re catching.

def is_jsonable(x):
    try:
        json.dumps(x)
        return True
    except (TypeError, OverflowError):
        return False

OverflowError is thrown when x contains a number which is too large for JSON to encode. A related answer can be found here.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)