0

I was doing this SQLAlchemy and pyodbc connection, but when I get my data from query it returns a tuple list without key, only value.

For example

[(valueOfObjectsSeparatedByComa), (valueOfObjectsSeparatedByComa), etc..]

This is my code.

from flask import Flask, request
from flask_restful import Resource, Api
import sqlalchemy as sa
from flask_sqlalchemy import SQLAlchemy
import json
from json import dumps
from flask.ext.jsonpify import jsonify
import pyodbc

client = pyodbc.connect('DRIVER={SQLSERVERDRIVER};SERVER=serverName;DATABASE=database;UID=user;PWD=password')

class Users(Resource):
    def get(self):
        query =("select * from SEC_User")
        try:
            result = client.execute(query).fetchall()
            return jsonify(result)
        except Exception as e:
            client.close()
            raise e


api.add_resource(Users, '/users') # Route_1

When I do a print(result) I can see the data in tuple format and only the value not the key, for example, instead of "name": "john" [(john), (james)] the correct would be [{name: jonh}, {name: james}].

What can I do to change the tuple data?

iled
  • 2,142
  • 3
  • 31
  • 43
  • Not sure if this is a duplicate of [How to serialize SqlAlchemy result to JSON?](https://stackoverflow.com/questions/5022066/how-to-serialize-sqlalchemy-result-to-json), or [Convert sqlalchemy row object to python dict](https://stackoverflow.com/questions/1958219/convert-sqlalchemy-row-object-to-python-dict). Both apply, though you're also using flask, so maybe [jsonify a SQLAlchemy result set in Flask](https://stackoverflow.com/questions/7102754/jsonify-a-sqlalchemy-result-set-in-flask)? – Ilja Everilä Mar 20 '18 at 21:16
  • i will try the "How to serialize SqlAlchemy result to JSON" i hope that works. – Ricardo Marquez Mar 20 '18 at 22:27
  • Possible duplicate of [How to serialize SqlAlchemy result to JSON?](https://stackoverflow.com/questions/5022066/how-to-serialize-sqlalchemy-result-to-json) – J.J. Hakala Mar 25 '18 at 09:12

0 Answers0