response is a 2d array with values for keys/columns ("test_id","querytext","metrics").
[0]['testid1','query1','"metrics": { "ndcg@3": "1.0", "ndcg@7": "0.9" }']
[1]['testid2','query2','"metrics": { "ndcg@3": "1.0", "ndcg@7": "0.9" }']
column_names=("test_id","querytext","metrics")
json_response={}
for entry in response:
if entry[0] not in json_response:
json_response[entry[0]]=[]
json_element={}
json_element[column_names[1]]=entry[1]
json_element[column_names[2]]=json.loads(entry[2])
json_response[entry[0]].append(json_element)
return json.dumps(json_response)
Now json_response will be of following format
{
"tesid1": [{
"querytext": "query1",
"metrics": {
"ndcg@3": "1.0",
"ndcg@7": "0.9"
}
}],
"testid2": [{
"querytext": "query2",
"metrics": {
"ndcg@3": "1.0",
"ndcg@7": "0.9"
}
}]
}
dict? Those things look very similar. JSON stands for JavaScript Object Notation which is a way to serialize data to exchange among systems. There isn't such a thing as a "JSON object" but there are objects in programming languages like python and javascript that can be built from them. Is this a sting and you want adictor is this adictand you want a string?