I’m fetching data from Mysql from table person(id, name, email). The table has many rows.
I’m trying to put into a json array in a loop. But in the json array it gets overwritten with the new array.
for row in results:
persons = {
[{
'personId' : row[0],
'personName' : row[1],
'personEmail' : row[2]
},]
}
print json.dumps(persons)
can anybody give a solution?
In your code, in each iteration of the for loop, you’re rebuilding persons as a Python dictionary with one member which is the current row, not adding more rows (“people”) to the dictionary. You need to rewrite that, something along the lines of :