pythonflaskjsonify

ERROR: TypeError: Object of type ColumnClause is not JSON serializable Python3


Hi i'm getting this error. TypeError: Object of type ColumnClause is not JSON serializable.

Whole thing:

    [2020-10-26 22:17:58,448] ERROR in app: Exception on /all-user [GET]
Traceback (most recent call last):
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\ryand\Desktop\mealplansfree\main-api\api.py", line 36, in decorated
    return f(*args, **kwargs)
  File "C:\Users\ryand\Desktop\mealplansfree\main-api\api.py", line 59, in get_all_users
    return jsonify({'users' : output})
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\json\__init__.py", line 370, in jsonify
    dumps(data, indent=indent, separators=separators) + "\n",
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\json\__init__.py", line 211, in dumps
    rv = _json.dumps(obj, **kwargs)
  File "c:\users\ryand\appdata\local\programs\python\python38-32\lib\json\__init__.py", line 234, in dumps
    return cls(
  File "c:\users\ryand\appdata\local\programs\python\python38-32\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "c:\users\ryand\appdata\local\programs\python\python38-32\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\json\__init__.py", line 100, in default
    return _json.JSONEncoder.default(self, o)
  File "c:\users\ryand\appdata\local\programs\python\python38-32\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ColumnClause is not JSON serializable

Here is the code for the above.

  @app.route('/all-user', methods=['GET'])
@application_required
def get_all_users():

    users = User.query.all()

    output = []

    for user in users:
        user_data = {}
        user_data['user_id'] = user.user_id
        user_data['full_name'] = user.full_name
        user_data['username'] = user.username
        user_data['password'] = user.password
        user_data['admin'] = user.admin
        output.append(user_data)

    return jsonify({'users' : output})

here is the secret key check

def application_required(f):
    @wraps(f)
    def decorated(*args, **kwargs):
        token = None

        if 'x-access-key' in request.headers:
            token = request.headers['x-access-key']

        if not token:
            return jsonify({'message' : 'ERROR x-access-key missing or incorrect.'}), 401

        if token == app.config['SECRET_KEY']:
            return f(*args, **kwargs)
        else:
            return jsonify({'message' : 'ERROR x-access-key missing or incorrect.'}), 401

    return decorated

If anyone knows whats going on or could guide me through whats going on and how to debug these that would be great!


Solution

  • I found the answer here: SQLAlchemy warning: column won't be part of the declarative mapping

    I had missed in my User model in the username it was a lowercase c and not upper case C. This fixed the error and is now working. Thanks!