TAGS :Viewed: 5 - Published at: a few seconds ago

[ python check if json.dumps is possible ]

is it possible to check if a value is able to be "json.dumps"d like json.dumping.possible(code) with a boolean output? I would be thankful for help

Answer 1


There is no way to check if an object is serializable to a json format. However, you can (even if it is not really EAFP compliant) try and if it fails go for a fallback process:

try:
    json.dumps(my_object)
except TypeError:
    print("Unable to serialize the object")

A way to be sure would be to implement your own JSONEncoder if we know your data structure.