I would like to create a JSON object structured as well:
{
"dataset": "xx",
"test": "trial",
"results": {
"TP": 5,
"FP": 1,
"FN": 1,
"TN": 2
}
}
I calculate these results in a loop on this way:
json_obj = {}
for i in range(len(dictionary)):
dataset, test = retrieve_data()
tp, fp, tn, fn = calculate_score()
json_obj = json.dumps({'dataset': dataset,
'test': test,
'results': {'TP': tp, 'FP': fp, 'FN': fn, 'TN': tn}})
Since I loop 4 times, I would expect a JSON object like this one:
{
"dataset": "1",
"test": "trial1",
"results": {
"TP": 5,
"FP": 3,
"FN": 2,
"TN": 5
},
"dataset": "2",
"test": "trial2",
"results": {
"TP": 6,
"FP": 4,
"FN": 12,
"TN": 25
},
"dataset": "3",
"test": "trial3",
"results": {
"TP": 15,
"FP": 1,
"FN": 11,
"TN": 2
},
"dataset": "4",
"test": "trial4",
"results": {
"TP": 5,
"FP": 11,
"FN": 1,
"TN": 21
}
}
Where if I access to the first element with the commands:
json_obj = json.dumps(json_obj)
print (json_obj[0])
I get
"dataset": "1",
"test": "trial1",
"results": {
"TP": 5,
"FP": 3,
"FN": 2,
"TN": 5
}
The problem is that if I run the code I just get " as output. if I print the full json_obj I get the string:
"{\"dataset\": \"1\", \"test\": \"trial1\", \"results\": {\"TP\": 5, \"FP\": 3, \"FN\": 2, \"TN\": 5}}"
It looks like it is creating a string instead of json object. Where is the error?