Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object of type ResultProxy is not JSON serializable: result in sqlalchemy query?

I am trying to query for 3 fields in a table like this in sqlalchemy:

if request.method == 'GET':
        search_form = SearchForm()
        result = dbSession.execute(
            "SELECT * FROM books WHERE (isbn LIKE '%:text%') OR (title LIKE '%:text%') OR (author LIKE '%:text%') LIMIT 10",
            { "text": search_form.searchText.data }
        )
        return jsonify({'result': result})

Is my query correct? why would i have this error?

TypeError: Object of type ResultProxy is not JSON serializable

like image 226
gpbaculio Avatar asked Sep 21 '18 19:09

gpbaculio


1 Answers

Simply error says result is not a dictionary. To solve it:

jsonify({'result': [dict(row) for row in result]})

It will convert each row to a dictionary.

like image 113
metmirr Avatar answered Oct 16 '22 17:10

metmirr