Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return Json from Flask-SQLAlchemy

I am not able to return a JSON string, although it is possible if I request just one entry.

This works

@app.route('/incomeexpense/<id>', methods=['GET'])
def handle_incomexpense(id):
    incomeexpense = IncomeExpenseModel.query.get_or_404(id)

    if request.method == 'GET':
            response = {
                    "orderdate": incomeexpense.orderdate,
                    "who": incomeexpense.who,
                    "location": incomeexpense.location,
                    "income": incomeexpense.income,
                    "expense": incomeexpense.expense
            }
            return {"message": "success", "incomeexpense": response}

This does not work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@app.route('/incomeexpense/all', methods=['GET'])
def handle_incomexpense_all():
    incomeexpense = IncomeExpenseModel.query.all();

    if request.method == 'GET':
            response = {
                    "orderdate": incomeexpense.orderdate,
                    "who": incomeexpense.who,
                    "location": incomeexpense.location,
                    "income": incomeexpense.income,
                    "expense": incomeexpense.expense
            }
            return {"message": "success", "incomeexpense": response}

Here is my model

class IncomeExpenseModel(db.Model):
    __tablename__ = 'incomeexpense'

    id = db.Column(db.Integer, primary_key=True)
    orderdate = db.Column(db.String())
    who = db.Column(db.String())
    location = db.Column(db.Integer())
    income = db.Column(db.Numeric(16,2), nullable=True)
    expense = db.Column(db.Numeric(16,2), nullable=True)

    def __init__(self, orderdate, who, location, income, expense):
            self.orderdate = orderdate
            self.who = who
            self.location = location
            self.income = income
            self.expense = expense

Maybe someone can tell me how to return a JSON string?

Thank you very much in advance.

>Solution :

incomeexpense = IncomeExpenseModel.query.all()
This line returns list of objects.

If you want to return JSON with one object you can use indexing, like:
incomeexpense = IncomeExpenseModel.query.all()[0]

Or If you want to return all of them, prepare response using loop:

responses = []
for incomeexpense in IncomeExpenseModel.query.all():
    responses.append({
        "orderdate": incomeepense.orderdate
        ...
    })
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading