Overriding the JSONEncoder class and calling o.result() in future instances is one way.
class CustomJSONEncoder(json.JSONEncoder): def default(self, o, *args, **kwargs): if isinstance(o, concurrent.futures.Future): return o.result() return super(CustomJSONEncoder, self).default(o, *args, **kwargs) json.dumps(data, cls=CustomJSONEncoder)
To make it work without a custom JSONEncoder, you can iterate through the data structure by calling o.result() :
data = [o.result() if isinstance(o, concurrent.futures.Future) else o for o in data]
Or change data in place:
for i, o in enumerate(data): if isinstance(o, concurrent.futures.Future): data[i] = o.result()
source share