@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.