0

I have the below code which is trying to return an object back to JSON. I'm trying to return back the MenuItem object to the caller

#Make JSON API
@app.route('/restaurant/<int:restaurant_id>/menu/<int:menu_id>/JSON')
def restaurantMenuJSONONE(restaurant_id,menu_id):
    item = session.query(MenuItem).filter_by(id = menu_id).one()
    return jsonify(MenuItems=item)

Error I'm getting:

TypeError: <database_setup.MenuItem object at 0xb6071d8c> is not JSON serializable

database_setup.py snips

class MenuItem(Base):
    __tablename__ = 'menu_item'

    name = Column(String(80), nullable=False)
    id = Column(Integer, primary_key=True)
    description = Column(String(250))
    price = Column(String(8))
    course = Column(String(250))
    restaurant_id = Column(Integer, ForeignKey('restaurant.id'))
    restaurant = relationship(Restaurant)

#JSON
    @property
    def serialize(self):
        #return object
        return {
            'name' : self.name,
            'description' : self.description,
            'id' : self.id,
            'price' : self.price,
            'course' : self.course,
        }
1
  • import json and return data before using json.dumps() Commented Apr 3, 2016 at 4:37

1 Answer 1

2

Solved it with:

return jsonify(MenuItem=item.serialize)

I also solved it with by creating a list and passing the list in, but its a lot more work than using serialize :)

Sign up to request clarification or add additional context in comments.

1 Comment

You can also have a look at stackoverflow.com/a/21411576/4620080 if you need a general way of solving this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.