The answer is a bit rudimentary but here goes
When the output is generated, store it to a variable which will be in the form of a python dictionary of input and output. Extract the output, which will be in a string, and use the json.loads function to transform it in a json format.