Prediction probabilities to JSON

I have a Python script to check an upladed image and everything seems to be working. The only thing is I would like to get the Prediction data into JSON format

Here is the code part and prediction:

pred_class,predict_idx,outputs = learn.predict(img)

saint_bernard	
tensor(30)	
tensor([1.2347e-03, 1.9417e-03, 6.8854e-05, 8.9688e-03, 5.6420e-04, 8.1800e-04,
        4.9134e-02, 1.4359e-02, 5.1173e-04, 1.3465e-04, 2.5928e-05, 4.3872e-06,
        2.6465e-04, 2.9790e-02, 2.3652e-03, 3.2989e-03, 3.1712e-05, 5.5037e-05,
        6.7967e-04, 7.6698e-04, 2.3886e-04, 1.4130e-02, 6.3727e-03, 8.1252e-04,
        3.4696e-03, 6.5521e-03, 1.0549e-03, 2.3085e-01, 7.5765e-03, 4.7727e-03,
        4.6957e-01, 3.6927e-03, 1.2897e-03, 1.3095e-01, 7.8122e-04, 2.6975e-03,
        1.7307e-04])

Would like the tensor(30) just be 30

and

tensor([1.2347e-03, 1.9417e-03, 6.8854e-05, etc…] ) to be JASON Array Format [“1.2347e-03”, “1.9417e-03”, “6.8854e-05”, “etc…”] without the line returns and spaces…

Thanks in advance…

I got it kinda working like I wanted. The main thing was to clean the “tensor()” parts. I created the following function to do that.

def cleanTensor(a):
    removeThis = ["tensor(", ")", "[", "]", "\r", "\n", " "]
    for x in removeThis:
        a = a.replace(x,"")
    return a	

For the second tensor, I converted it to and array after cleaning it…

outputsArray = outputs.split(',')

You can choose the prediction by:

this_predict = outputsArray[int(predict_idx)]

Note: I had to make predict_idx into an integer

Still playing around with it…