0

Currently I am working on a request that takes a database with the following class.

class Player(db.Model):
    id =  db.Column(db.Integer, primary_key=True)
    # ... etc

... and then I am trying to return all players in JSON format with the following function

class Players(Resource):
    def get(self):
        result = Player.query.all()
        return result

However so far I have not been able to get my result in JSON format. With the above code I get an error reading

Object of type Player is not JSON serializable

I have tried things like...

final_result = result.json()
return final_result

or ...

final_result = json.dump(result)
return final_result

neither of these solutions have worked for me. My end goals is to return all players in JSON format to first postman, and then my front end client when I am done with this project.

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
David Reke
  • 535
  • 1
  • 5
  • 20
  • Have you tried the solutions mentioned here - https://stackoverflow.com/questions/5022066/how-to-serialize-sqlalchemy-result-to-json – Chayan Bansal Jul 26 '21 at 15:37
  • 1
    Does this answer your question? [How to serialize SqlAlchemy result to JSON?](https://stackoverflow.com/questions/5022066/how-to-serialize-sqlalchemy-result-to-json) – Chayan Bansal Jul 26 '21 at 15:38
  • While that thread this one are similar they are not the same. In this instance I am trying to change a list into JSON format, and in that thread they are trying to change dict into json format. Those answers won't work here. – David Reke Jul 26 '21 at 17:29
  • @DavidReke just use `jsonify(Players.query.all())` + [app.json_encoder = CustomJSONEncoder](https://stackoverflow.com/questions/28544630/proper-overloading-of-json-encoding-and-decoding-with-flask) – Danila Ganchar Jul 27 '21 at 08:56

0 Answers0