In my flask app I am trying to practice taking data from a psql table and putting it in a dropdown. I am using this to get the data with psycopg2 using a query from here:
def load_authors(self):
# sql = "SELECT * FROM authors"
sql = "select array_to_json(array_agg(row_to_json(t))) from (select id, name from authors) t"
self.curs.execute(sql)
data = self.curs.fetchall()
print(data)
return data
This is my data from psql that I want to format into json:
[(1, 'Christopher Paolini'), (2, 'Marie Lu'), (3, 'John Flanagan')]
The previous code exports the data into this:
[([{'id': 1, 'name': 'Christopher Paolini'}, {'id': 2, 'name': 'Marie Lu'}, {'id': 3, 'name': 'John Flanagan'}],)]
It seems to be taking the correctly formatted json that I want:
[{'id': 1, 'name': 'Christopher Paolini'}, {'id': 2, 'name': 'Marie Lu'}, {'id': 3, 'name': 'John Flanagan'}]
And putting it within another list? I don't have a very deep understanding of this yet so I was wondering why it is doing this and how to fix it. I have tried multiple different queries but so far they all return the list within a list.